Skip to content
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

Fixing a memory leak #1099

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Fixing a memory leak #1099

wants to merge 1 commit into from

Commits on May 23, 2018

  1. Fixing a memory leak

    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();
    }
    Yann Andrey committed May 23, 2018
    Configuration menu
    Copy the full SHA
    81a4dcd View commit details
    Browse the repository at this point in the history