Skip to content

Instantly share code, notes, and snippets.

@nuitsjp
Last active December 13, 2015 03:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nuitsjp/b3b023ccb6974c2e8898 to your computer and use it in GitHub Desktop.
Save nuitsjp/b3b023ccb6974c2e8898 to your computer and use it in GitHub Desktop.
やりがちなアンマネージドメモリのリークと対処方法 ref: http://qiita.com/Nuits/items/9ce6a205a50bf8e3dbeb
IntPtr srcIntPtr = System.Runtime.InteropServices.Marshal.AllocCoTaskMem(length);
try
{
// ここに処理を記載する
}
finally
{
System.Runtime.InteropServices.Marshal.FreeCoTaskMem(srcIntPtr);
}
IntPtr srcIntPtr = IntPtr.Zero;
try
{
srcIntPtr = System.Runtime.InteropServices.Marshal.AllocCoTaskMem(length);
// ここに処理を記載する
}
finally
{
if(srcIntPtr != IntPtr.Zero)
{
System.Runtime.InteropServices.Marshal.FreeCoTaskMem(srcIntPtr);
}
}
System.Runtime.CompilerServices.RuntimeHelpers.PrepareConstrainedRegions();
try { }
finally
{
IntPtr srcIntPtr = System.Runtime.InteropServices.Marshal.AllocCoTaskMem(length);
try
{
// ここに処理を記載する
}
finally
{
System.Runtime.InteropServices.Marshal.FreeCoTaskMem(srcIntPtr);
}
}
IntPtr srcIntPtr = IntPtr.Zero;
try
{
System.Runtime.CompilerServices.RuntimeHelpers.PrepareConstrainedRegions();
try { }
finally
{
srcIntPtr = System.Runtime.InteropServices.Marshal.AllocCoTaskMem(length);
}
// ここに処理を記載する
}
finally
{
if(srcIntPtr != IntPtr.Zero)
{
System.Runtime.InteropServices.Marshal.FreeCoTaskMem(srcIntPtr);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment