I found a piece of code which surprised me. I thought it’s a good idea to start a post on it to make sure we are all on the same page.
If a function takes an output argument that is a
vector, it will require an initialized vector, and the caller is responsible for de-allocating that vector. It does not matter if the function returned successfully or with an error.
But what if the output argument is a
Take a look at what
- If the function returns successfully, it will leave it to the caller to free the result (naturally, as the caller will use it)
- If the function returns with an error (or gets interrupted), it will destroy each element of the
vector_ptr, but it will not free them, nor will it modify the
vector_ptrin any other way.
Let’s look at another function:
Here in case of an error all elements get destroyed, they also get freed, and the
vector_ptr gets cleared. This is different.
Here all elements get destroyed and freed, but the
vector_ptr is not cleared.
This is a bit of a mess. There should be a clear guidelines on how to deal with
I was just going to add interruption to
igraph_sir. It looks like not calling
igraph_i_sir_destroy() was on oversight. @Gabor you wrote this code originally. OK to free the elements in this function, or did I miss something?