[Stackless] how does my tasklets and objects get freed

Kristján Valur Jónsson kristjan at ccpgames.com
Sun Jan 31 00:46:37 CET 2010

I see.
Don't worry about the garbage collector.
Owning a reference to a python object on the C stack, or any where else, qualifies as "outside" for GC.  

And yes, PyTasklet_GetCurrent() probably does return a new reference.  I´d have to read the source to be sure.
Never the less, it is good practice when writing code with reference counting to stick to  conventional reference counting semantics.
The convention is, that a called function, if it wishes to hang on to the object, claims its own reference.  The Python api is mostly consistent about
1) return values being new references and
2) functions not stealing references
The exceptions are few, far between, and documented.
So, in pseude code, i´d advise you to do something like:
PyObject *current = PyStackless_GetCurrent()



> -----Original Message-----
> From: stackless-bounces at stackless.com [mailto:stackless-
> bounces at stackless.com] On Behalf Of Péter Szabó
> Sent: 29. janúar 2010 12:06
> To: The Stackless Python Mailing List
> Subject: Re: [Stackless] how does my tasklets and objects get freed
> > If there is no reference to the tasklet,
> There _is_ a reference (count) to the tasklet, because
> PyStackless_GetCurrent() calls Py_INCREF on its return value (I had a
> look at the Stackless source code).
> What I'm worried about is that even though Py_INCREF has been called,
> the garbage collector may free() the tasklet somehow, since it's not
> reachable from the root objects -- or is it? Does the local variable
> `tasklet' on the C stack of MyFunction (even when switched out)
> qualify as a root object, thus protecting the tasklet from being freed
> by the garbage collector?
> > how do you expect to restart it?
> event_set() registers a callback, which gets called later at some
> point with the argument (void*)tasklet. That callback will call
> PyTasklet_Insert(tasklet).
> > Looking at your code, obviously the call to event_set(), if it is to
> store the tasklet for later wakeup,
> > must do a Py_INCREF() on it.   Anyone intending to store a reference
> must Incref.
> I understand this. But isn't the Py_INCREF called by
> PyStackless_GetCurrent() enough? As far as I understand, a PY_INCREF
> is enough to protect the object from being free()ed by reference
> counting. But how does the object get protected from being free()ed by
> the garbage collector?
> _______________________________________________
> Stackless mailing list
> Stackless at stackless.com
> http://www.stackless.com/mailman/listinfo/stackless

More information about the Stackless mailing list