> On Tue, Nov 21, 2006 at 05:25:35PM +0100, Michael Meier wrote:
> > Hello
> > 
> > Is there any documentation about the memory management model in gnash?
> 
> Unfortunately not, but looking at the doxygen documentation
> (cd doc/C; make apidoc)

Can't build it, it says 
make: *** No rule to make target `apidoc'.  Stop.

> you can see which classes derive from ref_counted.
> Those classes do support ref-counting.
> When you construct a ref_counted class it's initial ref-count is 0.
> The reccomandation would be to always store allocated ref_counted
> objects to an intrusive_ptr:
> 
> boost::intrusive_ptr<as_array_object> array(new as_array_object());
> 
> This way you will be sure that the as_array_object will be deleted
> when your function returns *unless* somebody else did increment
> it's reference counter.
> 
> Note that it is *dengerous* not to use the intrusive_ptr. Consider
> the following:
> 
> 
>       void
>       do_something(as_object* obj)
>       {
>               // store in a smart pointer to keep it
>               // alive for whole duration of this function
>               {
>                       boost::intrusive_ptr<as_objecte> holdit(obj);
>                       // ref-count is incremented
>               }
> 
>               // out of scope, ref-count is decremented,
>               // if 0 obj is deleted !
>       }
> 
>       void
>       dangerous()
>       {
>               as_object* myobj = new as_object(); // ref-count == 0
>               do_something(myobj); // WARNING: myobj is not deleted !!!
>               as_object* proto = myobj->get_prototype();
>       }

? Isn't myobj deleted? The initial ref-count is 0. Then it is
incremented in do_something in order to the creation of a intrusive_ptr
instance. Then the gets out of scope, the ref-count is decremted, so
it's 0 again. Because it reaches zero, obj will be deleted. So void
dangerous is dangerous because it calls a method of myobj which got the
deleted. (the instance, at least) I'm confused :)


Thanks for the advice! I'll stick to intrusive_ptr from now on.

cheers,
Michael




_______________________________________________
Gnash-dev mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/gnash-dev

Reply via email to