igraph-help
[Top][All Lists]
Advanced

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

Re: [igraph] IGRAPH_FINALLY_CLEAN


From: Tamas Nepusz
Subject: Re: [igraph] IGRAPH_FINALLY_CLEAN
Date: Mon, 22 Feb 2010 13:43:55 +0000
User-agent: Mutt/1.5.20 (2009-06-14)

Hi,

> I am confused about how IGRAPH_FINALLY_CLEAN works.
> Perhaps someone (Gábor?) can help. My understanding
> is this: Some macros/functions register allocations
> on a stack. Eg, IGRAPH_VECTOR_INIT. Then, supose  an error occurs
> and an error hander is invoked before the funtion ends, and
> the temporary storage can be freed. In this case, the handler
> goes through the stack frees the storage. But this is only
> done on and error.
Your understanding of IGRAPH_FINALLY_CLEAN is right, this is what
happens behind the scenes.

> Much of the code seems to agree with this system. However, I
> don't always see this in the code; for instance in
> iterators.c, there is:
> 
>  int igraph_vs_as_vector(const igraph_t *graph, igraph_vs_t vs, 
>                       igraph_vector_t *v) {
>    igraph_vit_t vit;
>   
>    IGRAPH_CHECK(igraph_vit_create(graph, vs, &vit));
>    IGRAPH_FINALLY(igraph_vit_destroy, &vit);
>    IGRAPH_CHECK(igraph_vit_as_vector(&vit, v));
>   
>    IGRAPH_FINALLY_CLEAN(1);
>    return 0;
>  } 
> 
> Here you see there is no explicit call to free vit on a normal
> exit. Is this a bug in this function? Or is there something that
> I don't understand?
If I understand it right (and Gabor may confirm or correct me) is that
there is a bug indeed, because igraph_vit_t is not destroyed on a
successful exit. I don't know the reason but I guess it was simply
forgotten. However, note that there might be a legitimate reason for the
omission of the destructor in some cases. E.g., it might happen that
some function wants to construct a data structure that will be returned
via a pointer argument; in this case, there is a cleanup function
registered in the cleanup stack while the data structure is being built
(so it will be destroyed automagically should an error happen), but the
cleanup function does not have to be called when the data structure is
built successfully as it will be returned and the ownership of the data
structure will be transfered to the caller. This is a rare case, but
sometimes it happens, and this is the only case where you should call
IGRAPH_FINALLY_CLEAN() without calling the destructor itself.

-- 
Tamas






reply via email to

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