[Top][All Lists]

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

Re: [igraph] confused about vector pointer memory management

From: Tamas Nepusz
Subject: Re: [igraph] confused about vector pointer memory management
Date: Wed, 10 May 2017 17:16:05 +0200


In particular, I do not understand why igraph_vector_ptr_destroy_all()
will both destroy and free all items, but igraph_vector_ptr_clear()
will destroy them without freeing.
You are totally right -- igraph_vector_ptr_clear() should probably call the element destructors AND free the items. The problem is that when we have originally implemented pointer vectors, it did not have any item destructors at all; they were added later as an "afterthought", and we did not get this part right.

Actually, for me it seems like igraph_vector_ptr_destroy_all() and igraph_vector_ptr_free_all() should not have been necessary in the first place if we had item destructors from the very beginning. In that case, we could have simply said that igraph_vector_ptr_t vectors do not "own" their pointers and will not ever free them on their own -- all they could do is to call a "destructor" function on them when the items get out of the vector somehow. This way the user could have attached igraph_free as a destructor function for pointer vectors that claim ownership for any pointer added to them, and if the user needed something more complicated than free() (i.e. igraph_*_destroy() followed by igraph_free()), then this should have been done in a specialized destructor function on a case-by-case basis.

The best way to avoid confusion for the time being is probably not to use item destructors at all, or at least ensure that whenever you pass an igraph_vector_ptr_t to some part of the code that you don't control, then ensure that the vector does not have an item destructor and that the vector is empty. This way you won't run into any unexpected side effects.

I am leaning towards trying to fix this by ensuring that the item destructors get called when the items are removed from the vector using igraph_vector_ptr_clear(), but I'm not sure how this would affect third-party code. Personally, I don't use item destructors in the Python interface and the C interface also uses them sparingly and only in places where the pointer vectors equipped with destructors are not exposed to the user, so this shouldn't cause too much problems for the end users, except those who have been using the C interface directly and have used item destructors before.


reply via email to

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