[Pythonmac-SIG] PyObjC + AEInstallEventHandler crash?

Bob Ippolito bob at redivi.com
Wed Dec 21 05:09:20 CET 2005


> Bob Ippolito wrote:
>
>> > Trying to figure why the following test app crashes when a 'testtest'
>> > Apple event is sent to it:
>>
>>My guess is that there's a refcount or GIL bug in
>>Carbon.AE.AEInstallEventHandler.
>
> How would a GIL bug manifest itself? I always assumed it only became an
> issue with multiple threads, but it's not something I understand all that
> well.

When you release the GIL and call into Python before acquiring it again,
it'll almost definitely crash.  It doesn't matter if there are multiple
threads running, the threading machinery is probably active (it definitely
is if PyObjC is imported), which means that you'll have the threadstate as
NULL and you'll dereference a NULL pointer somewhere in Python machinery
-- boom.

>>I'd use NSAppleEventManager... I've used it before and it works just
>> fine.
>
> As I said, the reason I'm interested in why this example fails is because
> I get a very similar crash in very similar code when using PyObjC with
> CarbonX.OSA. (OSA.c's GenericSendFunction is cribbed from AE.c's
> GenericEventHandler.) I didn't show that code because it's a bit more
> complex, but that's the crash I'm really interested in fixing.

If my guess is right, you probably borrowed broken code :)  The handler
that  gets called into needs to do something like this.  There is an older
style of doing this with threadstates and whatnot, but the PyGILState API
is much easier to deal with.

void callback_from_c(void *data) {
    PyGILState_STATE state;
    PyObject *result;
    state = PyGILState_Ensure();
    /* call into Python */
    result = PyObject_CallFunctionObjArgs((PyObject *)data);
    /* clean up */
    if (result == NULL) {
        PyErr_Print();
        PyErr_Clear();
    } else {
        Py_DECREF(result);
    }
    PyGILState_Release(state);
}

-bob




More information about the Pythonmac-SIG mailing list