in reply to C++ object & PERL garbage collection

I'm not a C++ hacker, but I think I grok the problem. Presumably you have a Perl object which is wrapping a C++ object, and the Perl object's class has a DESTROY function which deletes the C++ object. You can override that DESTROY function and stop it from cleaning up the C++ object, but you'll end up with a memory leak. Another possibility is to save a local copy of the Perl object, using the perlapi function newSVsv, so that when the last Perl-space reference drifts out of scope, you still have a reference keeping it from being destroyed.

Caveat: this is a hard problem, and if you're not familiar with how and when DESTROY gets invoked and does its work, the above advice probably just gives you rope to hang yourself with.

GL,

--
Marvin Humphrey
Rectangular Research ― http://www.rectangular.com

Replies are listed 'Best First'.
Re^2: C++ object & PERL garbage collection
by diotalevi (Canon) on Sep 23, 2006 at 00:46 UTC

    I'm wondering whether the object isn't being constructed and blessed in C++ using XS. If that's the case then I guess I could see how this might be happening. I still really want to see the code to understand what kind of brokenness is occurring here.

    ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊


      1) we use SWIG, not XS the C++ object is a PERL HASH i can invokes methods on the object with OBJ->method() just like a PERL object! :-)

      exactly what code do you want to see?
      i sort of feel that this is a "philosophical" issue and not a "coding" mistake
      in another reply, i pointed out that OUR works.
      based on my limited understanding of OUR;
      it seems that OUR prevents PERL from deleting the C++ Object when its being passed around.
      but the OUR declaration has to be global
Re^2: C++ object & PERL garbage collection
by Anonymous Monk on Sep 23, 2006 at 01:34 UTC

    i have a perl HASH which is a SWIG wrapper of a C++ object.

    if i new the object, and pass the reference around
    it sometimes works and sometimes fails.
    if NEW is in MAIN and passed "down", it works if NEW is in ROUTINE and passed "up", it fails

    and hopefully my usage of up/down is clear!
    using namespaces :
    if new'd as MAIN::object, -> works in all "lower" routines if new'd as MAIN::routineA::object -> does NOT work in MAIN

    you have a interesting point about newSVsv. it looks like i want something like that.
    i need a mechanism for PERL to never delete an object
    but i wanted to avoid using a GLOBAL variable that is OUR
    maybe that is the purpose of OUR?

      I have to say, I'm very confused by your capitalization. Perl is case-sensitive. There is no OUR, there is only our. Similarly, when you refer to MAIN, I don't know whether you're referring to the default main namespace in Perl, or something having to do with C++ or SWIG.

      I can understand your English despite the miscapitalization of words like "I". However, your Perl/C++ leaves me baffled. Can you please reformulate this message to use proper capitalization and punctuation in both the English and the code?

      --
      Marvin Humphrey
      Rectangular Research ― http://www.rectangular.com