Re: How to add code to a C wrapper function generated by SWIG (for Python)
Ian Boyd <[email protected]> Fri, 22 Jan 2021 11:48:42 +0000
| Newsgroups | gmane.comp.programming.swig |
|---|---|
| Message-ID | <CAEofer43_Mrb7sANFiwJZOuC=EDZU1Pcskma8JeWOZwbVP9v-w@mail.gmail.com> |
From what I can see, implementing the destructor in an %extend results in a delete_<Class> function and a _wrap_delete<Class> function being generated in the Swig wrapper code. However, having taken a closer look, I see I did not remember all of this correctly. I was not %ignore'ing the destructor; the destructor was already private in the class being wrapped. Instead there was a public destroy method and it is _this_ that is being %ignore'd in the wrapper. Then the C++ destroy function is used to implement the destructor defined in the %extend section. So I do not know if this is at all useful for the question being asked here. On Fri, 22 Jan 2021 at 11:05, Rokas Kupstys via Swig-user < [email protected]> wrote: > And you have to call destructor manually instead of wrapper handling it? > > -- Rokas Kupstys > > On 2021-01-22 12:57, Ian Boyd wrote: > > Yes, that's the point - to prevent the generation of the wrapper. The > destructor is then implemented using %extend. > > The reason I remember this approach is that I had to fix up some code a > couple of years ago that had done something like this. I ran into issues > because of a Swig upgrade. There is a small thread in the archives: > https://sourceforge.net/p/swig/mailman/message/36465099/ > > It was someone else's code I was fixing up. I am not an expert on this, > and it might not be the right approach, but I thought I would mention in > case it might be useful in this case. > > On Fri, 22 Jan 2021 at 10:38, Rokas Kupstys via Swig-user < > [email protected]> wrote: > >> How would that work? %ignore would prevent generation of wrappers for >> ignored thing. I dont see how ignored destructor would then be called by >> wrapper code at appropriate time. >> >> -- Rokas Kupstys >> >> On 2021-01-22 12:12, Ian Boyd wrote: >> >> Another option I think I have seen before is to use %ignore to prevent >> Swig from generating a destructor and then use %extend to implement a >> custom one. >> >> On Fri, 22 Jan 2021 at 10:00, Rokas Kupstys via Swig-user < >> [email protected]> wrote: >> >>> Maybe %newobject + %newfree typemaps would work for you in this case? >>> >>> -- Rokas Kupstys >>> >>> On 2021-01-22 10:48, lille stor wrote: >>> >>> Hi, >>> >>> When generating a Python wrapper using SWIG, many C wrapper functions >>> are generated. One of them is called "_wrap_delete_Cursor" which looks >>> like this (I have stripped it to the bare minimum): >>> >>> SWIGINTERN PyObject *_wrap_delete_Cursor(PyObject *self, PyObject *args) { >>> >>> // some generated C code by SWIG >>> >>> // need to add code to call a C function to release allocated memory >>> >>> } >>> >>> This function is called when the object Cursor (created in Python) no >>> longer exists. Now, my problem is that I have some allocated memory on the >>> C side that needs to be released before existing the function (otherwise, >>> there will be memory leaks). >>> >>> Do you know how I can add a line of code this function to release >>> memory? All help is really welcome! >>> >>> L. >>> >>> >>> _______________________________________________ >>> Swig-user mailing [email protected]://lists.sourceforge.net/lists/listinfo/swig-user >>> >>> _______________________________________________ >>> Swig-user mailing list >>> [email protected] >>> https://lists.sourceforge.net/lists/listinfo/swig-user >>> >> _______________________________________________ >> Swig-user mailing list >> [email protected] >> https://lists.sourceforge.net/lists/listinfo/swig-user >> > _______________________________________________ > Swig-user mailing list > [email protected] > https://lists.sourceforge.net/lists/listinfo/swig-user > _______________________________________________ Swig-user mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/swig-user