?????? ?????

pointer to which is return by function like container_alloc().Initialization of each member may fail and if last fails, and I needto free() all other initialized resources (otherwise there would bememory leakage). The situation become worse when any random "sub-resource" initialization may fail.

What is best way to deal whis this? There is obvious solution to freeall previous allocated resources within "if" test block of eachelement, however I am looking for a better solution.

James Kuyper

9/16/2011 7:20:00 PM

On 09/16/2011 02:42 PM, Ð?Ð°ÐºÑÐ¸Ð¼ Ð¤Ð¾Ð¼Ð¸Ð½ wrote:> Hello.> > I have a question regarding following problem. Let there is> > struct Container{> void *buffer;> resource1 *res1;> resource2 *res2;> ...> resource_n *res_n;> };> > pointer to which is return by function like container_alloc().> Initialization of each member may fail and if last fails, and I need> to free() all other initialized resources (otherwise there would be> memory leakage). The situation become worse when any random "sub-> resource" initialization may fail.> > What is best way to deal whis this? There is obvious solution to free> all previous allocated resources within "if" test block of each> element, however I am looking for a better solution.

Set all of the pointers to null at start.If any allocation fails, free ALL of the pointers.Situations like this are part of the reason why the standard allows nullpointers to be passed to free().

Ben Bacarisse

9/16/2011 7:26:00 PM

Ð?Ð°ÐºÑÐ¸Ð¼ Ð¤Ð¾Ð¼Ð¸Ð½ <maxim@maxim-fomin.ru> writes:

> I have a question regarding following problem. Let there is>> struct Container{> void *buffer;> resource1 *res1;> resource2 *res2;> ...> resource_n *res_n;> };>> pointer to which is return by function like container_alloc().> Initialization of each member may fail and if last fails, and I need> to free() all other initialized resources (otherwise there would be> memory leakage). The situation become worse when any random "sub-> resource" initialization may fail.>> What is best way to deal whis this? There is obvious solution to free> all previous allocated resources within "if" test block of each> element, however I am looking for a better solution.

One solution is to initialise the struct first so that all the pointersare null. Then you can safely free everything because free(0) is ano-op. One way to do this is to keep a

ImpalerCore

9/16/2011 7:29:00 PM

On Sep 16, 2:42 pm, ?????? ????? <ma...@maxim-fomin.ru> wrote:> Hello.>> I have a question regarding following problem. Let there is>> struct Container{> void *buffer;> resource1 *res1;> resource2 *res2;> ...> resource_n *res_n;>> };>> pointer to which is return by function like container_alloc().> Initialization of each member may fail and if last fails, and I need> to free() all other initialized resources (otherwise there would be> memory leakage). The situation become worse when any random "sub-> resource" initialization may fail.>> What is best way to deal whis this? There is obvious solution to free> all previous allocated resources within "if" test block of each> element, however I am looking for a better solution.

christian.bau

9/16/2011 8:41:00 PM

> pointer to which is return by function like container_alloc().> Initialization of each member may fail and if last fails, and I need> to free() all other initialized resources (otherwise there would be> memory leakage). The situation become worse when any random "sub-> resource" initialization may fail.

You have a function container_alloc (), and you should have anotherfunction container_free (). container_alloc () returns a properlyinitialised container or NULL. The first thing it does is malloc () acontainer, and if that succeeds then it sets it up so thatcontainer_free () will work properly (for example by setting pointersto NULL). Then you allocate the pointers, check that they are allfine, and if not you pass the container to conter_free (). Randomfailures for sub resources is no problem; they would return NULL onfailure which you check for. It's easier if there is no need to stopafter a failure:

Malcolm McLean

9/16/2011 8:48:00 PM

On Sep 16, 9:42 pm, ?????? ????? <ma...@maxim-fomin.ru> wrote:> Hello.>> I have a question regarding following problem. Let there is>> struct Container{> void *buffer;> resource1 *res1;> resource2 *res2;> ...> resource_n *res_n;>> };>> pointer to which is return by function like container_alloc().> Initialization of each member may fail and if last fails, and I need> to free() all other initialized resources (otherwise there would be> memory leakage). The situation become worse when any random "sub-> resource" initialization may fail.>> What is best way to deal whis this? There is obvious solution to free> all previous allocated resources within "if" test block of each> element, however I am looking for a better solution.>I just write

res1 = allocateresource1();if(res1 == NULL) goto outofmemory;

....outofmemory: cleanupeverything; return NULL;

You sometimes have to be careful to set all the pointers to NULL, sothat they can be either passed to free harmlessly or detected asunallocated and not freed.--Basic algorithms, in C. Full JPEG codec.http://www.malcolmmclean.site...

By continuing, I agree that I am at least 13 years old and have read and agree to the Terms of Service and Privacy Policy.