Fixing a memory leak#1099
Open
Jaders77 wants to merge 1 commit into
Open
Conversation
This part of the code makes a copy of the original reference without calling the destructor at any time for it (original reference).
Instead of using a default constructor / copy constructor and then one destructor for each, such C++ copy elision optimization, it's better to simply remove copy.
Simple example of the problem:
public global::Lldb.SBSymbolContext GetSymbolContext(uint resolve_scope)
{
var __ret = new global::Lldb.SBSymbolContext.__Internal();
__Internal.GetSymbolContext((__Instance + __PointerAdjustment), new IntPtr(&__ret), resolve_scope); // here __ret is initialized like a constructor
return global::Lldb.SBSymbolContext.__CreateInstance(__ret);
}
internal static global::Lldb.SBSymbolContext __CreateInstance(global::Lldb.SBSymbolContext.__Internal native, bool skipVTables = false)
{
return new global::Lldb.SBSymbolContext(native, skipVTables);
}
private SBSymbolContext(global::Lldb.SBSymbolContext.__Internal native, bool skipVTables = false)
: this(__CopyValue(native), skipVTables)
{
__ownsNativeInstance = true;
NativeToManagedMap[__Instance] = this;
}
private static void* __CopyValue(global::Lldb.SBSymbolContext.__Internal native)
{
var ret = Marshal.AllocHGlobal(sizeof(global::Lldb.SBSymbolContext.__Internal));
global::Lldb.SBSymbolContext.__Internal.cctor(ret, new global::System.IntPtr(&native));
return ret.ToPointer();
}
After the fix:
private static void* __CopyValue(global::Lldb.SBSymbolContext.__Internal native)
{
var ret = Marshal.AllocHGlobal(sizeof(global::Lldb.SBSymbolContext.__Internal));
*(global::Lldb.SBSymbolContext.__Internal*) ret = native;
return ret.ToPointer();
}
|
|
ddobrev
force-pushed
the
master
branch
12 times, most recently
from
October 15, 2018 05:15
0464938 to
637018f
Compare
ddobrev
suggested changes
Nov 9, 2018
ddobrev
left a comment
Contributor
There was a problem hiding this comment.
The generated code now does not call the non-trivial constructor if any. I think this might lead to incorrect behaviours on the native side.
ddobrev
force-pushed
the
master
branch
4 times, most recently
from
January 4, 2019 21:27
91e219c to
32da859
Compare
tritao
force-pushed
the
master
branch
2 times, most recently
from
March 11, 2019 00:30
a0169c2 to
3ea7e97
Compare
ddobrev
force-pushed
the
master
branch
2 times, most recently
from
April 10, 2019 23:04
a1559de to
304d673
Compare
ddobrev
force-pushed
the
master
branch
2 times, most recently
from
June 26, 2019 21:06
a08e24b to
0e1fb0b
Compare
ddobrev
force-pushed
the
master
branch
5 times, most recently
from
April 15, 2020 22:32
1610aa5 to
64b1efd
Compare
ddobrev
force-pushed
the
master
branch
2 times, most recently
from
January 2, 2021 20:41
c930b78 to
c38556a
Compare
ddobrev
force-pushed
the
main
branch
7 times, most recently
from
August 30, 2021 11:25
4c1e9b8 to
2fdd082
Compare
ddobrev
force-pushed
the
main
branch
3 times, most recently
from
October 2, 2021 15:17
bcf41e4 to
851ec5e
Compare
ddobrev
force-pushed
the
main
branch
5 times, most recently
from
October 29, 2021 19:56
97610ec to
a2aeaed
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This part of the code makes a copy of the original reference without calling the destructor at any time for it (original reference).
Instead of using a default constructor / copy constructor and then one destructor for each, such C++ copy elision optimization, it's better to simply remove copy.
Simple example of the problem:
After the fix: