From 904e528dd9096f015458093ac149d172bad0e743 Mon Sep 17 00:00:00 2001 From: William S Fulton Date: Fri, 10 Jun 2005 23:29:58 +0000 Subject: [PATCH] Fix early garbage collection for alternative char * typemaps git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@7275 626c5289-ae23-0410-ae9c-e8d60b6d4f22 --- Lib/csharp/csharp.swg | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/Lib/csharp/csharp.swg b/Lib/csharp/csharp.swg index 8ae9a2a1d..2a1805338 100644 --- a/Lib/csharp/csharp.swg +++ b/Lib/csharp/csharp.swg @@ -653,22 +653,22 @@ using System.Runtime.InteropServices; /* // Alternative char * typemaps. -// Warning the GC may collect the SWIGStringMarshal instance while the unmanaged code is executing, so these typemaps should be avoided. %pragma(csharp) imclasscode=%{ - public class SWIGStringMarshal { - public readonly IntPtr ptr; + public class SWIGStringMarshal : IDisposable { + public readonly HandleRef swigCPtr; public SWIGStringMarshal(string str) { - ptr = System.Runtime.InteropServices.Marshal.StringToHGlobalAnsi(str); + swigCPtr = new HandleRef(this, System.Runtime.InteropServices.Marshal.StringToHGlobalAnsi(str)); } - ~SWIGStringMarshal() { - System.Runtime.InteropServices.Marshal.FreeHGlobal(ptr); + public virtual void Dispose() { + System.Runtime.InteropServices.Marshal.FreeHGlobal(swigCPtr.Handle); + GC.SuppressFinalize(this); } } %} -%typemap(imtype) char *, char[ANY], char[] "IntPtr" +%typemap(imtype, out="IntPtr") char *, char[ANY], char[] "HandleRef" %typemap(out) char *, char[ANY], char[] %{ $result = $1; %} -%typemap(csin) char *, char[ANY], char[] "new $modulePINVOKE.SWIGStringMarshal($csinput).ptr" +%typemap(csin) char *, char[ANY], char[] "new $modulePINVOKE.SWIGStringMarshal($csinput).swigCPtr" %typemap(csout, excode=SWIGEXCODE) char *, char[ANY], char[] { string ret = System.Runtime.InteropServices.Marshal.PtrToStringAnsi($imcall);$excode return ret;