python.exe crash and ctypes use

Discussion in 'Python' started by waldek, Jul 23, 2008.

  1. waldek

    waldek Guest

    Hi,

    I have module A.py and B.dll which exports C functions by cdecl_

    In A.py I pass callback (py callable) to dll. Next, thread inside dll
    simply calls my callback (in a loop). After few secs I got crash of
    python.exe.

    How to debug it?

    I'm using winxp and py 2.5.2


    ===========================
    def mycallback(data, size)
    return 0

    CBFUNC = CFUNCTYPE(c_int,POINTER(c_int), c_int)
    dll = cdll.mydll

    if dll.RegisterCallback(CBFUNC(mycallback)) != 0:
    print "Error."
    ===========================
     
    waldek, Jul 23, 2008
    #1
    1. Advertising

  2. waldek wrote:

    > Hi,
    >
    > I have module A.py and B.dll which exports C functions by cdecl_
    >
    > In A.py I pass callback (py callable) to dll. Next, thread inside dll
    > simply calls my callback (in a loop). After few secs I got crash of
    > python.exe.
    >
    > How to debug it?
    >
    > I'm using winxp and py 2.5.2
    >
    >
    > ===========================
    > def mycallback(data, size)
    > return 0
    >
    > CBFUNC = CFUNCTYPE(c_int,POINTER(c_int), c_int)
    > dll = cdll.mydll
    >
    > if dll.RegisterCallback(CBFUNC(mycallback)) != 0:
    > print "Error."
    > ===========================


    Attach a debugger of choice to your python-process, and see what happens.

    Diez
     
    Diez B. Roggisch, Jul 23, 2008
    #2
    1. Advertising

  3. waldek schrieb:
    > Hi,
    >
    > I have module A.py and B.dll which exports C functions by cdecl_
    >
    > In A.py I pass callback (py callable) to dll. Next, thread inside dll
    > simply calls my callback (in a loop). After few secs I got crash of
    > python.exe.
    >
    > How to debug it?
    >
    > I'm using winxp and py 2.5.2
    >
    >
    > ===========================
    > def mycallback(data, size)
    > return 0
    >
    > CBFUNC = CFUNCTYPE(c_int,POINTER(c_int), c_int)
    > dll = cdll.mydll
    >
    > if dll.RegisterCallback(CBFUNC(mycallback)) != 0:
    > print "Error."
    > ===========================


    You need the callback function instance - what the CBFUNC(mycallback)
    call returns - alive as long as some C code is calling it.
    If you don't sooner or later the Python garbage collector will
    free it since it seems to be no longer used. ctypes does NOT keep
    the callback function alive itself.

    Thomas
     
    Thomas Heller, Jul 23, 2008
    #3
  4. waldek

    waldek Guest

    Re: python.exe crash and ctypes use

    On Jul 23, 4:39 pm, Thomas Heller <> wrote:
    > waldek schrieb:
    >
    >
    >
    > > Hi,

    >
    > > I have module A.py and B.dll which exports C functions by cdecl_

    >
    > > In A.py I pass callback (py callable) to dll. Next, thread inside dll
    > > simply calls my callback (in a loop). After few secs I got crash of
    > > python.exe.

    >
    > > How to debug it?

    >
    > > I'm using winxp and py 2.5.2

    >
    > > ===========================
    > > def mycallback(data, size)
    > > return 0

    >
    > > CBFUNC = CFUNCTYPE(c_int,POINTER(c_int), c_int)
    > > dll = cdll.mydll

    >
    > > if dll.RegisterCallback(CBFUNC(mycallback)) != 0:
    > > print "Error."
    > > ===========================

    >
    > You need the callback function instance - what the CBFUNC(mycallback)
    > call returns - alive as long as some C code is calling it.
    > If you don't sooner or later the Python garbage collector will
    > free it since it seems to be no longer used. ctypes does NOT keep
    > the callback function alive itself.
    >
    > Thomas


    In fact ctypes does not keep references to callback passed directly to
    dll. Now it works.
    Thanks Thomas.

    Waldek
     
    waldek, Jul 24, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Henk Punt
    Replies:
    0
    Views:
    403
    Henk Punt
    Jul 23, 2004
  2. Java and Swing

    SWIG - python.exe crash

    Java and Swing, Oct 4, 2005, in forum: Python
    Replies:
    0
    Views:
    405
    Java and Swing
    Oct 4, 2005
  3. Chaos
    Replies:
    1
    Views:
    420
  4. Replies:
    0
    Views:
    506
  5. Replies:
    1
    Views:
    80
    Tim Golden
    Jul 3, 2013
Loading...

Share This Page