Re: callback from C DLL returning an int [SEC=PERSONAL]
RayS <[email protected]> Thu, 14 Jul 2011 21:09:13 -0700
| Newsgroups | gmane.comp.python.ctypes |
|---|---|
| Message-ID | <20110715040939.ESMX21362.fed1rmfepo202.cox.net@fed1rmimpo02.cox.net> |
At 05:52 PM 7/14/2011, Andrew MacIntyre wrote:
>From: RayS
>
> > My ctypes attempt is crashing the wxPython app.
> > This is how SetCallback is defined in the DLL:
> >
> > extern "C" void __declspec(dllexport) SetCallback( long lAddress ) {
> > s_frontend.set_callback((FUNCPTR)lAddress );
> > }
> > //The function takes in a 32-bit long pointer to a function.
> > //The function when defined in a calling C program should take in
> one parameter such as:
> >
> > void ASLocalCode::call_back( int iMessage )
> >
> > //This is fired when a new block of data is available. iMessage
> integer value is always 6.
> >
> > In Python I did this:
> > ______________________________________
> > ...
> > class myADC:
> > def __init__:
> > ...
> > self.driver = cdll.dataDll
> > ...
> > def data_ready_func(r):
> > print r
> >
> > CB_TYPE = CFUNCTYPE(None, c_int)
> > self.data_ready_ref = CB_TYPE(data_ready_func)
> > print CB_TYPE, self.data_ready_ref
> > self.driver.SetCallback(self.data_ready_ref)
> >
> > This prints:
> > data_ready_func 6
> > then XP pops up a notice "python.exe has encountered a
> problem" (the app is still running!), then it all closes with the
> XP dialog "don't send" button.
> > What is wrong here?
>
>The only thing that looks to me like a potential problem is that you
>don't keep an explicit reference to the callback function - the
>"def" just creates it at runtime in the scope of the __init__().
>
>I'd try adding something like
>
> self.callback = data_ready_func
>
>just after defining the callback, to test this theory.
Thanks Andrew,
I had tried making the callback a module or class level function as
well, and, I also made the callback function reference a class
attribute etc. - nothing helps.
I just jin'd the following simplest possible code, which shows that
the crash is upon the return of the callback:
- if the callback function sleep() is longer than the last sleep()
then the second print in the callback is not done, the program exits
before the callback is done, and no error
- if the callback sleep is less the function completes, Windoze
freaks, and the app finishes execution!
This also points out that the callback does not block, is it outside the GIL?
The c callback is fired ~1/40 second after StartAcquisition.
Enthought Py2.7 distro, BTW
____________________________________________
import ctypes
import time
import os
def py_data_ready_func(r):
print "in py_data_ready_func 1", r
time.sleep(1)
#time.sleep(5)
print "in py_data_ready_func 2"
driver = ctypes.cdll.VBridge
print 'Connect()', driver.Connect()
CB_TYPE = ctypes.CFUNCTYPE(None, ctypes.c_int)
data_ready_ref = CB_TYPE(py_data_ready_func)
driver.SetCallback(data_ready_ref) #boom
print 'start', driver.StartAcquisition()
print 'data started'
time.sleep(3)
print 'done'
_________________________________________
Results:
-long callback sleep-
C:\projects>python test4.py
Connect() 0
start 0
data started
in py_data_ready_func 1 6
in py_data_ready_func 2
done
-shorter callback sleep-
C:\projects>python test4.py
Connect() 0
blockSize 0
start 0
data started
in py_data_ready_func 1 6
done
Best,
Ray
------------------------------------------------------------------------------
AppSumo Presents a FREE Video for the SourceForge Community by Eric
Ries, the creator of the Lean Startup Methodology on "Lean Startup
Secrets Revealed." This video shows you how to validate your ideas,
optimize your ideas and identify your business strategy.
http://p.sf.net/sfu/appsumosfdev2dev
_______________________________________________
ctypes-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ctypes-users