Fixed memory leaks by calling clearOrphan() on removed XML nodes. #3592
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.
XML-related memory leaks were reported when loading OpenSim models. Specifically, deserializing Rajagopal2015.osim causes numerous memory leaks. See issue #3537.
I used WinCRT to help find the leaks. Since WinCRT is platform specific (i.e., runs on Windows) and requires some changes to the source code to leak test effectively, this PR only includes the fixes (not the modified code that I used to identify the leaks and verify the fixes).
None of the observed leaks associated with deserializing Rajagopal2015.osim were due to coding errors in SimTK::Xml. SimTK::Xml looks like it is working fine. (@adamkewley and I did find a constructor in SimTK::Xml that should be revised, but it is not currently the source of leaks that I can identity).
The reported memory leaks were all occurring in OpenSim in various
updateFromXMLNode()
calls. In particular,updateFromXMLNode()
forOpenSim::Body
,OpenSim::Coordinate
,OpenSim::Muscle
,OpenSim::PhysicalFrame
, andOpenSim::WrapObject
.All of the leaks were in code related to converting from an older version of OpenSim (i.e., from v3.0) to the current version. Thus, the memory leaks can be sidestepped by converting old models to the current version (by deserializing them) and then saving to a new .osim model file.
This sort of sidestepping doesn't address a situation in which a old version model is repeatedly deserialized, however.
Fortunately, the leaks were not too difficult to find. In each case, an element was removed from the XML document and was not inserted elsewhere in the model hierarchy. In all these cases, the fix was simply to call
clearOrphan()
on the element that was removed.Rajagopal2015.osim, whether represented in v3.0 or in the current version, is now deserializing without memory leaks.
Fixes issue #3537
Brief summary of fixes
In the files listed below, I inserted a call to
clearOrphan()
. To make these calls easier to find, amidst all the trailing whitespaces that were removed, I've included the line numbers of the inserted calls.Testing I've completed
CHANGELOG.md (choose one)
This change is