Closed migueldeicaza closed 3 weeks ago
re: the lookupObject issue, could you just check the ref count to see if it's already referenced elsewhere?
You never know who owns the references, you just know someone has claimed them.
I am not worried, both of those are very simple fixes, I just need a few hours to look into it, will get it fixed soon.
For the second leak, I almost have a patch, but I need both the tests, and add an additional scenario: currently when we are surfacing Framework peers (those that have no state of ours) we set the "ownsHandle" to false, and I think that we need to change that.
I humbly suggest that commit d1d6ca4 is incorrect. PR #524 proposes a different fix, and adds a fix for the second leak, and test cases for both leaks.
You are absolutely right.
I threw away my half cooked patch, I was just starting to realize we needed different code paths to handle some variants and going back to the drawing board when I saw both this comment and your lovely contribution.
Thank you!
I recently revamped the memory management for SwiftGodot to deal with the RefCounted scenario that needed to have its reference initialized, and it also solved a leak that was caused in code like this:
While the new code is a vast design improvement over the previous setup, it regressed a few scenarios that could have been either band-aids or where the ownership protocol contract has been broken, and I need to review those.
First Leak
This one is because the indexer is creating a Variant by making a copy of it, but it looks like the indexer call itself already made a copy. The change was done in the past to solve this issue: https://github.com/migueldeicaza/SwiftGodot/issues/390
Second Leak
The case is:
This is caused by the additional call to
reference
inasObject
:I could be wrong, and will research more next week when I am back, but I think that the code above is partially right in needing the reference, the issue is that lookupObject does two things, it performs a lookup on an object that is known (this currently should only return an existing value for user-defined types since we are tracking their lifetime) but create a fresh instance that is owned otherwise - so the reference is being added to an object that is already referenced in that case.
The additional
rc.reference
was introduced in 4d3f632dcdb2d7f0fb492c89e16a586ee43ce0d8My hunch is that I need a version of lookupObject that tells me if this is a freshly created wrapper, or if this returning an object that we knew about (and thus already has a reference), and based on that call the rc.reference or not.
Tests that I should write: