guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Is my procedure getting GCed?


From: Brett Viren
Subject: Re: Is my procedure getting GCed?
Date: Tue, 1 May 2001 14:00:08 -0400 (EDT)

Yes, I figured as much.  Essentially, I must treat an SCM value which
is held by a C++ object on the heap similarly to how I treat a pointer
to the C++ object itself.  So, when when one mixes Guile and C/C++,
one must worry about memory leaks on both sides of the Guile--C/C++
fence.  I didn't come across this issue in the ref or tut.

Since I read in gc.c that it is okay to nest protect/unprotects I do
so in the C++ object's constructor/destructor as well as when I do any
assignments (as you suggest).  The SCM object will be ``over''
protected if the C++ object is on the stack, but this shouldn't be a
problem, if I understand correctly.

To avoid all this by-hand protecting/unprotecting I instead could
handle this by accessing all SCM values via a lightweight C++ Handler
class and deal with protect/unprotect by overriding operator=().

Thanks,
-Brett.

Bill Gribble writes:
 > On Tue, May 01, 2001 at 07:59:01PM +0300, Michael Livshin wrote:
 > > scm_protect_object (fProc); // once fProc is intialized, of course
 > > scm_unprotect_object (fProc);
 > 
 > This may be more Ritual Incantation than reasoned action, but I
 > believe you also need to make sure to call unprotect/protect when you
 > assign a new value to the C struct slot:
 > 
 > struct foo {
 >    SCM bar;
 > };
 > 
 > [...]
 >    SCM  new_val;
 >    foo  * bax;
 >    
 >    /* scm_protect_object should have been called in the initializer
 >     * for 'bax' */ 
 >    scm_unprotect_object(bax->bar);
 >    bax->bar = new_val;
 >    scm_protect_object(bax->bar);
 > 
 > b.g.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]