iTunes共享库中带有PyObject_Call()的分段错误 [英] Segmentation-fault with PyObject_Call() in shared library for iTunes

查看:78
本文介绍了iTunes共享库中带有PyObject_Call()的分段错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试iTunes SDK和Cython. DLL入口点似乎可以正常工作,但是使用任何真实的Python" 都会导致iTunes崩溃.

I'm experimenting with the iTunes SDK and Cython. The DLL entry-point seems to work, but using any "real Python" causes iTunes to crash.

下面的代码可以正常编译,并且iTunes已成功加载plugin-dll.

The following code compiles fine and the plugin-dll is loaded successfully by iTunes.

cimport libc.stdio      as stdio

cdef extern from "iTunesAPI/iTunesAPI.h":

    ctypedef int    OSType
    ctypedef int    OSStatus

    ctypedef struct PluginMessageInfo:
        pass

    int             unimpErr

cdef public OSStatus iTunesPluginMain( OSType               message,
                                       PluginMessageInfo*   messageInfo,
                                       void*                refCon ):
    cdef stdio.FILE*    fl = stdio.fopen('C:/itunes_cyplugin_feedback.txt', 'wb')
    return unimpErr

但是添加Python表达式会导致iTunes崩溃,例如:

But adding a Python-expression will cause iTunes to crash, e.g:

# ...

cdef public OSStatus iTunesPluginMain( OSType               message,
                                       PluginMessageInfo*   messageInfo,
                                       void*                refCon ):
    object()
    return unimpErr

您是否知道为什么会导致崩溃?

操作系统:Windows 7 x64
iTunes :10.5 x86
编译器:gcc 4.6.2(MinGW)

OS: Windows 7 x64
iTunes: 10.5 x86
Compiler: gcc 4.6.2 (MinGW)

编辑[2012/02/12]

新见解给了我 gdb 会话,您可以在下面找到.我希望有人现在可以告诉我如何解决它. :)

New insights gave me the gdb session, which you can find below. I hope someone can now tell me how to fix it. :)

C:\Program Files (x86)\iTunes>gdb iTunes.exe
GNU gdb (GDB) 7.3.1
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "mingw32".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from c:\program files (x86)\itunes\iTunes.exe...(no debugging symbols found)...done.
(gdb) b write
No symbol table is loaded.  Use the "file" command.
Make breakpoint pending on future shared library load? (y or [n]) y
Breakpoint 1 (write) pending.
(gdb) r
Starting program: c:\program files (x86)\itunes\iTunes.exe
[New Thread 4588.0xa20]
[New Thread 4588.0x1214]
[New Thread 4588.0x12f4]
[New Thread 4588.0x274]
[New Thread 4588.0x1070]
[New Thread 4588.0xf40]
[New Thread 4588.0xfb0]
[New Thread 4588.0x12b0]
[New Thread 4588.0xb48]
[New Thread 4588.0x58c]
[New Thread 4588.0xc7c]
[New Thread 4588.0x1048]
[New Thread 4588.0x8ac]
[New Thread 4588.0x10dc]
[New Thread 4588.0x190]
[New Thread 4588.0x128]
[New Thread 4588.0xfbc]
[New Thread 4588.0xc2c]
[New Thread 4588.0x1154]
[New Thread 4588.0x234]
[New Thread 4588.0xa70]
[New Thread 4588.0x1144]
[New Thread 4588.0xa48]
[New Thread 4588.0xbe8]
warning: ASL checking for logging parameters in environment variable "iTunes.exe.log"

warning: ASL checking for logging parameters in environment variable "asl.log"

BFD: C:\Windows\SysWOW64\WMVCORE.DLL: Warning: Ignoring section flag IMAGE_SCN_MEM_NOT_PAGED in section .reloc

Program received signal SIGSEGV, Segmentation fault.
0x1e0650bc in python27!PyObject_Call () from C:\Windows\SysWOW64\python27.dll
(gdb)
(gdb) bt all
No symbol "all" in current context.
(gdb) bt
#0  0x1e0650bc in python27!PyObject_Call () from C:\Windows\SysWOW64\python27.dll
#1  0x02eb12ae in iTunesPluginMain (__pyx_v_message=1768843636, __pyx_v_messageInfo=0x16e6b0, __pyx_v_refCon=0x0)
    at pytunes.c:465
#2  0x68a4d627 in iTunes!getPhaseStreamLibraryVersion () from c:\program files (x86)\itunes\iTunes.dll
#3  0x696e6974 in iTunesMainEntryPoint@24 () from c:\program files (x86)\itunes\iTunes.dll
#4  0x00000000 in ?? ()
(gdb)

这是Visual Studio 2008告诉我的:

And this is what Visual Studio 2008 tells me:

Unhandled exception at 0x1e0650bc in iTunes.exe: 0xC0000005
Segmentation fault when reading at 0x00000004.


编辑[2012/02/13]

以下是iTunes崩溃后(gdb) thread apply all bt的输出.

The following is the output of (gdb) thread apply all bt after iTunes crashed.

(gdb) thread apply all bt

Thread 24 (thread 4340.0x4e0):
#0  0x76e564f4 in ntdll!LdrFindResourceEx_U () from C:\Windows\system32\ntdll.dll
#1  0x76e55e6c in ntdll!ZwWriteVirtualMemory () from C:\Windows\system32\ntdll.dll
#2  0x7504179c in WaitForSingleObjectEx () from C:\Windows\system32\KernelBase.dll
#3  0x0000067c in ?? ()
#4  0x00000000 in ?? ()

Thread 23 (thread 4340.0x17a0):
#0  0x76e564f4 in ntdll!LdrFindResourceEx_U () from C:\Windows\system32\ntdll.dll
#1  0x76178f8f in UnregisterPowerSettingNotification () from C:\Windows\system32\user32.dll
#2  0x59bd3485 in iTunesMobileDevice!AFCConnectionGetStatus ()
   from C:\Program Files\Common Files\Apple\Mobile Device Support\iTunesMobileDevice.dll
#3  0x06fef87c in ?? ()
#4  0x00000000 in ?? ()

Thread 22 (thread 4340.0x10c4):
#0  0x76e564f4 in ntdll!LdrFindResourceEx_U () from C:\Windows\system32\ntdll.dll
#1  0x76e55e7c in ntdll!ZwYieldExecution () from C:\Windows\system32\ntdll.dll
#2  0x76e4067b in ntdll!RtlLargeIntegerShiftLeft () from C:\Windows\system32\ntdll.dll
#3  0x76511174 in KERNEL32!AcquireSRWLockExclusive () from C:\Windows\system32\kernel32.dll
#4  0x76e6b3f5 in ntdll!RtlInsertElementGenericTableAvl () from C:\Windows\system32\ntdll.dll
#5  0x76e6b3c8 in ntdll!RtlInsertElementGenericTableAvl () from C:\Windows\system32\ntdll.dll
#6  0x00000000 in ?? ()

Thread 21 (thread 4340.0x14ec):
#0  0x76e564f4 in ntdll!LdrFindResourceEx_U () from C:\Windows\system32\ntdll.dll
#1  0x76e55e6c in ntdll!ZwWriteVirtualMemory () from C:\Windows\system32\ntdll.dll
#2  0x7504179c in WaitForSingleObjectEx () from C:\Windows\system32\KernelBase.dll
#3  0x0000063c in ?? ()
#4  0x00000001 in ?? ()
#5  0x06edf8c0 in ?? ()
#6  0x73ae556e in CreatePropertySheetPageA ()
   from C:\Windows\WinSxS\x86_microsoft.windows.common-controls_6595b64144ccf1df_6.0.7600.16385_none_421189da2b7fabfc\co
mctl32.dll
#7  0x7650f003 in WaitForSingleObjectEx () from C:\Windows\system32\kernel32.dll
#8  0x765d6172 in ole32!CoRevokeClassObject () from C:\Windows\system32\ole32.dll
#9  0x765dc80b in ole32!CoIsOle1Class () from C:\Windows\system32\ole32.dll
#10 0x76511174 in KERNEL32!AcquireSRWLockExclusive () from C:\Windows\system32\kernel32.dll
#11 0x76e6b3f5 in ntdll!RtlInsertElementGenericTableAvl () from C:\Windows\system32\ntdll.dll
#12 0x76e6b3c8 in ntdll!RtlInsertElementGenericTableAvl () from C:\Windows\system32\ntdll.dll
#13 0x00000000 in ?? ()

Thread 20 (thread 4340.0x1628):
#0  0x76e564f4 in ntdll!LdrFindResourceEx_U () from C:\Windows\system32\ntdll.dll
#1  0x76e55e6c in ntdll!ZwWriteVirtualMemory () from C:\Windows\system32\ntdll.dll
#2  0x7504179c in WaitForSingleObjectEx () from C:\Windows\system32\KernelBase.dll
#3  0x00000650 in ?? ()
#4  0x00000001 in ?? ()
#5  0x00000000 in ?? ()

Thread 19 (thread 4340.0xf04):
#0  0x76e564f4 in ntdll!LdrFindResourceEx_U () from C:\Windows\system32\ntdll.dll
#1  0x76e55e6c in ntdll!ZwWriteVirtualMemory () from C:\Windows\system32\ntdll.dll
#2  0x7504179c in WaitForSingleObjectEx () from C:\Windows\system32\KernelBase.dll
#3  0x000005c0 in ?? ()
#4  0x00000001 in ?? ()
#5  0x00000000 in ?? ()

Thread 18 (thread 4340.0x3c4):
#0  0x76e564f4 in ntdll!LdrFindResourceEx_U () from C:\Windows\system32\ntdll.dll
#1  0x76e55e4c in ntdll!ZwWriteRequestData () from C:\Windows\system32\ntdll.dll
#2  0x75046872 in KERNELBASE!GetProcessId () from C:\Windows\system32\KernelBase.dll
#3  0x00000001 in ?? ()
#4  0x068af914 in ?? ()
#5  0x00000001 in ?? ()
#6  0x00000000 in ?? ()

Thread 17 (thread 4340.0x880):
#0  0x76e564f4 in ntdll!LdrFindResourceEx_U () from C:\Windows\system32\ntdll.dll
#1  0x76e55e4c in ntdll!ZwWriteRequestData () from C:\Windows\system32\ntdll.dll
#2  0x75046872 in KERNELBASE!GetProcessId () from C:\Windows\system32\KernelBase.dll
#3  0x00000001 in ?? ()
#4  0x0665f758 in ?? ()
#5  0x00000001 in ?? ()
#6  0x00000001 in ?? ()
#7  0x00000000 in ?? ()

Thread 16 (thread 4340.0x165c):
#0  0x76e564f4 in ntdll!LdrFindResourceEx_U () from C:\Windows\system32\ntdll.dll
#1  0x76e55e4c in ntdll!ZwWriteRequestData () from C:\Windows\system32\ntdll.dll
#2  0x75046872 in KERNELBASE!GetProcessId () from C:\Windows\system32\KernelBase.dll
#3  0x00000001 in ?? ()
#4  0x05e2f5a8 in ?? ()
#5  0x00000001 in ?? ()
#6  0x00000000 in ?? ()

Thread 14 (thread 4340.0xb6c):
#0  0x76e564f4 in ntdll!LdrFindResourceEx_U () from C:\Windows\system32\ntdll.dll
#1  0x76e55e6c in ntdll!ZwWriteVirtualMemory () from C:\Windows\system32\ntdll.dll
#2  0x7504179c in WaitForSingleObjectEx () from C:\Windows\system32\KernelBase.dll
#3  0x00000580 in ?? ()
#4  0x00000001 in ?? ()
#5  0x063cfe08 in ?? ()
#6  0x737f53a6 in ?? ()
#7  0x5cea92e4 in ?? ()
#8  0x00000580 in ?? ()
#9  0x0000001e in ?? ()
#10 0x00000024 in ?? ()
#11 0x00000001 in ?? ()
#12 0x00000000 in ?? ()

Thread 13 (thread 4340.0xc74):
#0  0x76e564f4 in ntdll!LdrFindResourceEx_U () from C:\Windows\system32\ntdll.dll
#1  0x76e55e6c in ntdll!ZwWriteVirtualMemory () from C:\Windows\system32\ntdll.dll
#2  0x7504179c in WaitForSingleObjectEx () from C:\Windows\system32\KernelBase.dll
#3  0x00000548 in ?? ()
#4  0x00000001 in ?? ()
#5  0x00000000 in ?? ()

Thread 12 (thread 4340.0x778):
#0  0x76e564f4 in ntdll!LdrFindResourceEx_U () from C:\Windows\system32\ntdll.dll
#1  0x76e55e7c in ntdll!ZwYieldExecution () from C:\Windows\system32\ntdll.dll
#2  0x76e4067b in ntdll!RtlLargeIntegerShiftLeft () from C:\Windows\system32\ntdll.dll
#3  0x76511174 in KERNEL32!AcquireSRWLockExclusive () from C:\Windows\system32\kernel32.dll
#4  0x76e6b3f5 in ntdll!RtlInsertElementGenericTableAvl () from C:\Windows\system32\ntdll.dll
#5  0x76e6b3c8 in ntdll!RtlInsertElementGenericTableAvl () from C:\Windows\system32\ntdll.dll
#6  0x00000000 in ?? ()

Thread 10 (thread 4340.0x1620):
#0  0x76e564f4 in ntdll!LdrFindResourceEx_U () from C:\Windows\system32\ntdll.dll
#1  0x76e54c1c in ntdll!ZwDeleteValueKey () from C:\Windows\system32\ntdll.dll
#2  0x75041876 in SleepEx () from C:\Windows\system32\KernelBase.dll
#3  0x00000000 in ?? ()

Thread 9 (thread 4340.0x10cc):
#0  0x76e564f4 in ntdll!LdrFindResourceEx_U () from C:\Windows\system32\ntdll.dll
#1  0x76e55e7c in ntdll!ZwYieldExecution () from C:\Windows\system32\ntdll.dll
#2  0x76e4067b in ntdll!RtlLargeIntegerShiftLeft () from C:\Windows\system32\ntdll.dll
#3  0x76511174 in KERNEL32!AcquireSRWLockExclusive () from C:\Windows\system32\kernel32.dll
#4  0x76e6b3f5 in ntdll!RtlInsertElementGenericTableAvl () from C:\Windows\system32\ntdll.dll
#5  0x76e6b3c8 in ntdll!RtlInsertElementGenericTableAvl () from C:\Windows\system32\ntdll.dll
#6  0x00000000 in ?? ()

Thread 8 (thread 4340.0xac4):
#0  0x76e564f4 in ntdll!LdrFindResourceEx_U () from C:\Windows\system32\ntdll.dll
#1  0x76e55e7c in ntdll!ZwYieldExecution () from C:\Windows\system32\ntdll.dll
#2  0x76e4067b in ntdll!RtlLargeIntegerShiftLeft () from C:\Windows\system32\ntdll.dll
#3  0x76511174 in KERNEL32!AcquireSRWLockExclusive () from C:\Windows\system32\kernel32.dll
#4  0x76e6b3f5 in ntdll!RtlInsertElementGenericTableAvl () from C:\Windows\system32\ntdll.dll
#5  0x76e6b3c8 in ntdll!RtlInsertElementGenericTableAvl () from C:\Windows\system32\ntdll.dll
#6  0x00000000 in ?? ()

Thread 7 (thread 4340.0xe08):
#0  0x76e564f4 in ntdll!LdrFindResourceEx_U () from C:\Windows\system32\ntdll.dll
#1  0x76e55e4c in ntdll!ZwWriteRequestData () from C:\Windows\system32\ntdll.dll
#2  0x75046872 in KERNELBASE!GetProcessId () from C:\Windows\system32\KernelBase.dll
#3  0x00000001 in ?? ()
#4  0x057df9f0 in ?? ()
#5  0x00000001 in ?? ()
#6  0x00000000 in ?? ()

Thread 6 (thread 4340.0x7fc):
#0  0x76e564f4 in ntdll!LdrFindResourceEx_U () from C:\Windows\system32\ntdll.dll
#1  0x76e55e7c in ntdll!ZwYieldExecution () from C:\Windows\system32\ntdll.dll
#2  0x76e4067b in ntdll!RtlLargeIntegerShiftLeft () from C:\Windows\system32\ntdll.dll
#3  0x76511174 in KERNEL32!AcquireSRWLockExclusive () from C:\Windows\system32\kernel32.dll
#4  0x76e6b3f5 in ntdll!RtlInsertElementGenericTableAvl () from C:\Windows\system32\ntdll.dll
#5  0x76e6b3c8 in ntdll!RtlInsertElementGenericTableAvl () from C:\Windows\system32\ntdll.dll
#6  0x00000000 in ?? ()

Thread 5 (thread 4340.0x61c):
#0  0x76e564f4 in ntdll!LdrFindResourceEx_U () from C:\Windows\system32\ntdll.dll
#1  0x76e55e4c in ntdll!ZwWriteRequestData () from C:\Windows\system32\ntdll.dll
#2  0x76e3ef27 in ntdll!RtlIsValidIndexHandle () from C:\Windows\system32\ntdll.dll
#3  0x76511174 in KERNEL32!AcquireSRWLockExclusive () from C:\Windows\system32\kernel32.dll
#4  0x76e6b3f5 in ntdll!RtlInsertElementGenericTableAvl () from C:\Windows\system32\ntdll.dll
#5  0x76e6b3c8 in ntdll!RtlInsertElementGenericTableAvl () from C:\Windows\system32\ntdll.dll
#6  0x00000000 in ?? ()

Thread 4 (thread 4340.0x1794):
#0  0x76e564f4 in ntdll!LdrFindResourceEx_U () from C:\Windows\system32\ntdll.dll
#1  0x76e55e4c in ntdll!ZwWriteRequestData () from C:\Windows\system32\ntdll.dll
#2  0x75046872 in KERNELBASE!GetProcessId () from C:\Windows\system32\KernelBase.dll
#3  0x00000002 in ?? ()
#4  0x04e8f9e8 in ?? ()
#5  0x00000001 in ?? ()
#6  0x00000000 in ?? ()

Thread 2 (thread 4340.0x51c):
#0  0x76e564f4 in ntdll!LdrFindResourceEx_U () from C:\Windows\system32\ntdll.dll
#1  0x76e55e6c in ntdll!ZwWriteVirtualMemory () from C:\Windows\system32\ntdll.dll
#2  0x7504179c in WaitForSingleObjectEx () from C:\Windows\system32\KernelBase.dll
#3  0x00000218 in ?? ()
#4  0x00000000 in ?? ()

Thread 1 (thread 4340.0x17b4):
#0  0x1e0650bc in python27!PyObject_Call () from C:\Windows\system32\python27.dll
#1  0x69781232 in iTunesPluginMain (__pyx_v_message=1768843636, __pyx_v_msgInfo=0x13e680, __pyx_v_refCon=0x0)
    at pytunes.c:467
#2  0x5bc1d217 in iTunes!getPhaseStreamLibraryVersion () from C:\Program Files\iTunes\iTunes.dll
#3  0x696e6974 in ?? ()
#4  0x0013e680 in ?? ()
#5  0x00000000 in ?? ()
(gdb)


编辑[2012/02/13]

我只是想到了为什么不在C语言中尝试呢?,所以我做到了.而且它也不起作用,iTunes仍然崩溃.也许我应该将问题重命名为如何将Python嵌入iTunes?" ..

I just got the idea Why not try it in C?, and so I did. And it doesn't work either, iTunes still crashes. Maybe I should rename the question to "How to embed Python into iTunes?" ..

EDIT :现在,纯C实现不会导致iTunes崩溃,但是我不知道上次做错了什么.

EDIT: The pure C implementation now does not cause iTunes to crash, but I dunno what I did wrong last time.

#include "iTunesAPI/iTunesAPI.h"
#include <Python.h>

__declspec(dllexport) OSStatus iTunesPluginMain(OSType              message,
                                                PluginMessageInfo*  msgInfo,
                                                void*               refCon) {
    Py_Initialize();
    PyObject* op = Py_BuildValue("i", 123);
    Py_Finalize();
    return unimpErr;
}

推荐答案

看到回溯之后,我看不到其他任何python调用.然后我想到一个主意:由于您试图用Cython(Python)做一个iTunes插件(C),因此Python解释器可能未初始化.您需要创建一个用于引导python解释器的中间C文件,并在实际使用cython模块之前对其进行初始化:

After seeing the backtrace, i don't see any other python call. And then an idea came in my head: since you are trying to do a itunes plugin (C) with Cython (Python), the Python interpreter might not be initialized. You need to create an intermediate C file for bootstraping the python interpreter and initialize the cython module before actually using it:

在main.c中:

#include <Python.h>
#include "../iTunesAPI/iTunesAPI.h"
#include "pytunes.c"

__declspec(dllexport) OSStatus iTunesPluginMain( OSType             message,
                                                 PluginMessageInfo* msgInfo,
                                                 void*              refCon ) {
    Py_Initialize();
    PyEval_InitThreads();
    initpytunes();
    return PyTunes_main(message, msgInfo, refCon);
}

在您的pytunes.pyx中:

And in your pytunes.pyx:

cdef extern from "../iTunesAPI/iTunesAPI.h":

    ctypedef int        OSType
    ctypedef int        OSStatus

    ctypedef struct PluginMessageInfo:
        pass

    int unimpErr

cdef public int PyTunes_main(   OSType               message,
                                PluginMessageInfo*   msgInfo,
                                void*                refCon ):
    object()
    return 0

现在,在调用cython/c函数之前,将安全地初始化python解释器和cython扩展:)

Now the python interpreter will be safely initialized, and cython extension too, before calling the cython/c function :)

这篇关于iTunes共享库中带有PyObject_Call()的分段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆