If I declare this inside a PIKECLASS in a .cmod, I need the canonical:
PIKEFUN void setfoo(void|object foo) { if(THIS->foo) free_object(THIS->foo); if((THIS->foo=foo)) { debug_malloc_touch(THIS->foo); Pike_sp--; /* don't decrease references */ } else pop_stack(); }
to set it.
The question is, should I also free_object() it in the destructor, or does Pike already take care of that somewhere?
Stephen R. van den Berg wrote:
The question is, should I also free_object() it in the destructor, or does Pike already take care of that somewhere?
Apparently Pike already takes care of this. Doing so manually resulted in a SEGV (sometimes).
Marcus Comstedt (ACROSS) (Hail Ilpalazzo!) @ Pike (-) developers forum wrote:
{ if(THIS->foo) free_object(THIS->foo);
You can use
do_free_object(THIS->foo);
instead.
Which helps in what way? Since, as far as I understand it, Pike already frees it somewhere, or is that a flawed assumption? Could it be that Pike does not free it until program termination?
Huh? It helps by not forcing you to do the NULL-test yourself, since do_free_object() does it for you. That is the only difference between free_object() and do_free_object(). (That, and the fact that free_object causes wanton inlining...)
If I declare this inside a PIKECLASS in a .cmod, I need the canonical:
PIKEFUN void setfoo(void|object foo) { if(THIS->foo) free_object(THIS->foo); if((THIS->foo=foo)) { debug_malloc_touch(THIS->foo); Pike_sp--; /* don't decrease references */ } else pop_stack(); }
to set it.
The question is, should I also free_object() it in the destructor, or does Pike already take care of that somewhere?
It depends on whether THIS->foo is a mapped variable (ie PIKEVAR) or not. If it is mapped, then you don't need to (and if you do, you need to set it to NULL). If it isn't then you should free it in the exit callback.
BTW: In the instant case, you can probably use
assign_short_svalue((union anything *)THIS->foo, (union anything *)foo, PIKE_T_OBJECT); pop_stack();
"Stephen R. van den Berg" srb@cuci.nl wrote:
if((THIS->foo=foo)) { debug_malloc_touch(THIS->foo);
I suggest replacing this bit with the move_svalue macro:
if (foo) { move_svalue (THIS->foo, foo);
It has some debug stuff to detect if the old svalue is used later by mistake, but more importantly it's good for code clarity.
Martin Stjernholm wrote:
"Stephen R. van den Berg" srb@cuci.nl wrote:
if((THIS->foo=foo)) { debug_malloc_touch(THIS->foo);
I suggest replacing this bit with the move_svalue macro:
if (foo) { move_svalue (THIS->foo, foo);
It has some debug stuff to detect if the old svalue is used later by mistake, but more importantly it's good for code clarity.
Erm, these are not svalues, but objects, so move_svalue() cannot be used. Any other suggestions?
"Stephen R. van den Berg" srb@cuci.nl wrote:
Erm, these are not svalues, but objects, so move_svalue() cannot be used. Any other suggestions?
No, for that there aren't any fancy macros that I'm aware of.
pike-devel@lists.lysator.liu.se