Skip to content
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

[PYTHON] Ensure numpy array increments refcount of tensor to keep valid #219

Merged
merged 3 commits into from
May 15, 2021

Conversation

axsaucedo
Copy link
Member

Take the follwing example

m = kp.Manager()
 
t = m.tensor([1,2,3])

td = t.data()

del t

td
<invalid memory>

Currently if a numpy array is fetched from the tensor, the memory ownership is currently not managed correctly. This PR ensures that the numpy array created incrememnts the refcount of the sharedptr for the Tensor object, to ensure that the numpy array can be checked for validity before it's accessed or modified.

With the update in this PR it can be done as follows:

m = kp.Manager()
 
t = m.tensor([1,2,3])

td = t.data()

del t

td
# this is OK

assert td.base.is_init() == True # OK

m.destroy() # Frees all memory inside tensors

assert td.base.is_init() == False # Consistent to expected setup

del td # Now this calls tensor destructor as refcount reaches 0

@axsaucedo axsaucedo merged commit a3d8b78 into master May 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant