Re: Add specific code to SWIG C# generated 'Dispose' method

William S Fulton <[email protected]> Sat, 30 Jan 2021 19:31:56 +0000
Newsgroups gmane.comp.programming.swig
Message-ID <CANGqftBRPnHEbG_Kq5HmDGZnMtNQF46uRgBFyEuhsexE9P54TA@mail.gmail.com>
On Thu, 21 Jan 2021 at 13:44, lille stor <[email protected]> wrote:

> Hi,
>
> I'm using SWIG to generate a C# wrapper of my C code. I can see that SWIG generates this method when the C# object is about to be disposed/finalized:
> *public virtual void Dispose() {
>     lock(this)
>     {
>         if (swigCPtr.Handle != global::System.IntPtr.Zero)
>         {
>
>             // *** add specific code here to call C function to release allocated memory ***
>
>             if (swigCMemOwn)
>             {
>                 swigCMemOwn = false;
>                 TestPINVOKE.delete_TestCursor(swigCPtr);
>             }
>             swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
>         }
>         global::System.GC.SuppressFinalize(this);
>     }
>  }*
>
>
> However, I need to release allocated memory from the C side before the C#
> object is disposed/finalized (otherwise there is memory leaks on the C
> side). I have been looking at the available documentation but can't reach
> at any solution. The idea that I have is to call a C function to release
> the memory (please notice the comment embedded in the above code snippet).
>
> All help is really appreciated. Thanks!
>
>
See my reply I just sent out for your query on the Java equivalent. You'd
need to provide equivalent C# typemaps of course instead of Java typemaps,
see the table at http://swig.org/Doc4.0/CSharp.html#CSharp_differences_java.
For C# we have some void * pointer marshalling typemaps which will be
useful for you. They marshal System.IntPtr to void *. See the void
*VOID_INT_PTR typemaps in csharp.swg.

William

_______________________________________________
Swig-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/swig-user