MXRoomState/MXRoomMembers: Fix memory leak and copying #1040
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
MXRoomState
andMXRoomMembers
currently hold mutual strong references to each other via themembers
property and thestate
ivar, respectively. This leads to a memory leak preventing both objects from being deallocated. The leak is easy to reproduce in the Element iOS app. Just launch the app, use a few rooms and start the memory graph debugger. At this point you'll find orphaned objects retaining each other such as:Additionally, the leak is masqueraded by a bug in
MXRoomState
'scopyWithZone:
method. The latter copies theMXRoomMembers
object but doesn't update itsstate
ivar to point to theMXRoomState
copy. As a result, copying anMXRoomState
object does not introduce another leak. I've noticed this when writing a unit test for the leak. Instead of the retain cycle, there now is a chain whereMXRoomMembers
is owned by one room state but points "back" to another.The present pull request addresses both issues. First, the
state
ivar onMXRoomMembers
is made weak. This seems safe becauseMXRoomState
retains itsmembers
property andMXRoomMembers
should not exist on its own without a room state owning it.Secondly, a new method
copyWithZone:andState:
is introduced onMXRoomMembers
. The latter copies the receiver and updates thestate
ivar to the specifiedMXRoomState
.Pull Request Checklist