This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author theller
Recipients gholling, theller
Date 2009-04-07.18:14:50
SpamBayes Score 2.158346e-06
Marked as misclassified No
Message-id <1239128094.08.0.802500503723.issue5710@psf.upfronthosting.co.za>
In-reply-to
Content
There is a problem returning arbitrary complicated ctypes types from
callbacks.  Consider that a callback function returns a structure; the
structure itself may contain 'char *' field for example.  The callback
function creates the structure and fills in the fields.  The 'char *'
field contains a pointer to a nul-terminated string; the ctypes
structure object must keep the Python string alive as long as the
structure itself.  It does this via the private '_objects' instance
variable.  If the callback function now returns the structure, the
Python ctypes object usually will go out of scope and will be garbage
collected, together with its '_objects'.  So, the pointer(s) contained
in the structure will become invalid now, and even simple structure
fields will become invalid.

However, the callback function result will be used by some other code
and will surely crash.

In principle it should be possible to make code like this work; one
would have to maintain the returned objects elsewhere and clean them up
when they are no longer used anywhere; but I think this gets too
complicated.
History
Date User Action Args
2009-04-07 18:14:55thellersetrecipients: + theller, gholling
2009-04-07 18:14:54thellersetmessageid: <1239128094.08.0.802500503723.issue5710@psf.upfronthosting.co.za>
2009-04-07 18:14:52thellerlinkissue5710 messages
2009-04-07 18:14:51thellercreate