-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
prevent premature deletion #64
Comments
Do you have a link to some documentation/gist/blog/stackoverflow for something like this? The current behavior is unfortunate and I agree, unexpected. |
Python destroys objects at some point after they are no longer referenced. In your case this means that when calling parasail.sw_trace("asdf", "asdf", 11, 1, parasail.blosum62).cigar.seq the object is destroyed after the call to parasail.sw_trace("asdf", "asdf", 11, 1, parasail.blosum62).cigar since the Result object returned by To prevent this you need to make sure the object is still referenced in there, so class Cigar:
def __init__(self, result):
self._result = result
class Result:
def __init__(self, pointer, ...):
self.pointer = pointer
def __del__(self):
_lib.parasail_result_free(self.pointer)
def test(self):
return Cigar(self) or if you do not want to pass the whole object around it can be achieved using an additional object which wraps the memory (essentially this is similar to something like a numpy array) class Cigar:
def __init__(self, pointer):
self._pointer = pointer
class _ResultPointer:
def __init__(self, pointer, ...):
self.pointer = pointer
def __del__(self):
_lib.parasail_result_free(self.pointer)
class Result:
def __init__(self, pointer, ...):
self.pointer = _ResultPointer(pointer)
def test(self):
return Cigar(self.pointer) |
The readme mentions:
This is really bad, since this is generally a very unexpected behaviour. The behavior can be prevented by adding another level of abstraction around pointers. Currently you have e.g. the following implementation:
this should be changed to something like this:
this abstraction around the pointer ensures that the memory will not be freed to early.
The text was updated successfully, but these errors were encountered: