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

BF: Fixed memory leak in QuickBundles. #684

Merged
merged 1 commit into from Jul 20, 2015

Conversation

Projects
None yet
3 participants
@MarcCote
Contributor

MarcCote commented Jul 18, 2015

This PR fixes a memory leak that was present in ClusterCentroid used by the QuickBundles algorithm. The __dealloc__ function was missing a free statement for a variable. In addition to that, references count of memoryview objects were not decremented automatically/correctly when freeing the allocated memory. I found that assigning them to None (before freeing the memory) works.

This PR also add a testing tool that retrieves the refcount of a given object type. This tool is used in the new unit test function that check there are no memory leaks in QuickBundles.

@matthew-brett

This comment has been minimized.

Member

matthew-brett commented Jul 18, 2015

Thanks for doing this. Is the memoryview problem a Cython bug? Does it need reporting?

@MarcCote

This comment has been minimized.

Contributor

MarcCote commented Jul 18, 2015

I wouldn't say it is a bug, it is more related to the way I'm using them. I needed a variable number of memoryviews. In Cython, you can't have pointers of memoryviews so I hacked my way around using a struct that has a memorview as attribute.

I'm using malloc to allocate memory for those struct of memoryviews and use free accordingly. This implies there are no refcount inc/decrements. However, as soon as I assign a memoryview to something, a refcount increment is done. At first, I wasn't award of this so I didn't take the necessary precaution in the __dealloc__ function. Once I realized that a refcount decrement wasn't done correctly I searched a lot for a Cython function allowing me to decrement the refcount of a memoryview (which doesn't required a pointer because we cannot take the address of a memoryview). I finally found that assigning them to None does the trick.

@matthew-brett

This comment has been minimized.

Member

matthew-brett commented Jul 18, 2015

Thanks for the explanation.

@Garyfallidis

This comment has been minimized.

Member

Garyfallidis commented Jul 18, 2015

Good catch! 👯

@MarcCote

This comment has been minimized.

Contributor

MarcCote commented Jul 18, 2015

Thanks to @martcous to bringing this memory leak to my attention in the first place.

Garyfallidis added a commit that referenced this pull request Jul 20, 2015

Merge pull request #684 from MarcCote/memleaks_qb
BF: Fixed memory leak in QuickBundles.

@Garyfallidis Garyfallidis merged commit eb6ea58 into nipy:master Jul 20, 2015

1 check passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details

@MarcCote MarcCote deleted the MarcCote:memleaks_qb branch Jul 20, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment