brunoalano / pyswip

Automatically exported from code.google.com/p/pyswip
MIT License
0 stars 0 forks source link

Callbacks can cause segv's #8

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Use callbacks
2. See if it's a GC "bad hair day"

PySWIP creates a wrapper function around the foreign callback, and then wraps 
it in the CFUNCTYPE callback wrapper.  The problem is that it passes this down 
to the Prolog implementation, but does not keep a local ref.  It thus becomes a 
collectable object in Python, and can be written over by subsequent uses.

Meanwhile the memory is still registered by Prolog for a callback, and it when 
invoked will jump into memory whose contents might have been arbitrarily 
rewritten.

So you need to keep a ref on your callback wrapper at the Python level.  
Otherwise callbacks will cause segv's or other such craziness at random times.

Original issue reported on code.google.com by vandy...@gmail.com on 30 Jun 2010 at 11:01

GoogleCodeExporter commented 9 years ago
Can you give a small example and the work around?

Thanks,
dksr

Original comment by dksre...@gmail.com on 3 Aug 2010 at 3:02

GoogleCodeExporter commented 9 years ago
Really, all you have to do is invoke a foreign function.  But I'll attach what 
I have to help you get started.  Note we've moved on from SWI and PySwip, and 
are using a Prolog interpreter written in Python, which (obviously) obviates 
the whole issue.

nondet.py is some code which hooks SWI into Python generators; a very 
convenient way to interface to the Prolog engine.

easy.py has changes to try and keep the Python GC aware of data structure 
references.

try2.py is the only test code I could find.

Original comment by vandy...@gmail.com on 12 Sep 2010 at 10:15

Attachments:

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
Issue 16 has been merged into this issue.

Original comment by rodrigo....@gmail.com on 28 Dec 2012 at 5:56

GoogleCodeExporter commented 9 years ago
jpthompson23 patch in r119 solved this issue.

Original comment by rodrigo....@gmail.com on 29 Dec 2012 at 12:21