zproject icon indicating copy to clipboard operation
zproject copied to clipboard

Python_cffi we can't use automatic destructors?

Open sphaero opened this issue 6 years ago • 6 comments

Take Zmsg for example. If we construct a message and want to send it. The sender becomes the owner and will destroy the message. However if we use garbage collection it will also try to destroy the message resulting in an assert.

I think the only solution is, is to stop using GC and do manual destruction.

Parking this here so I can work on this later.

sphaero avatar May 15 '19 07:05 sphaero

Either we remove garbage collection and only do manual cleanups or we add a method to remove cffi's gc() if we are passed to a method with "by_reference=1"

@sappo any thoughts on this?

sphaero avatar May 15 '19 14:05 sphaero

Uff...this is tricky.

So here's one thought. I do build separate wrapper c-library which wraps the destroy calls so the signature is compatible to what ffi.gc expects as destructor. We could add some logic to these wrapper functions so that the actual destroy function is only called if we have something to destroy.

You can find the wrapper library in the build.py file.

sappo avatar May 15 '19 18:05 sappo

But removing the gc function entirely would be the cleaner approach: https://cffi.readthedocs.io/en/latest/ref.html#ffi-gc ffi.gc(ptr, None, size=0)

sappo avatar May 15 '19 18:05 sappo

IMO, the zmsg_send is defined wrongfully in the API file. It should be defined as destructor which will allow us to remove the cffi's gc function.

This might break some bindings though or at least remove the send function from those. Still I believe that's the correct solution here!

sappo avatar May 15 '19 18:05 sappo

Ow, indeed that would be nicest. But it would also mean this issue needs to move czmq

sphaero avatar May 15 '19 20:05 sphaero

Yes, this issue is also valid e.g. for the JNI binding.

sappo avatar May 16 '19 09:05 sappo