L
lallous
Hello,
This question was asked in comp.lang.c++ and the answers involved the use of
objects whose destructors are automatically called when getting out of
scope, however I was expecting suggestions that doesn't involve the use of
objects. So here is the question again.
I have a function like:
void fnc() {
char *mem1, *mem2, *mem3, *mem4;
FILE *fp1;
// lots of code...
// mem1, 2, 3, 4 got allocated
// lots of code and condition checks
if (condition_failed)
{
// blah blah
// free mem1, mem2, mem3, mem4
return;
}
if (condition2_failed)
{
// blah blah
// free mem1, mem2, ...
fclose(fp1);
return;
}
// here the end of routine (clean exit code):
// free mem1, mem2, mem3, mem4
}
Usually, I would use compiler specific solution by putting most of the code
in __try() and the clean exit code in _finally() block then to reach the
clean exit code I would invoke __leave.
Or use lables and then goto clean_exit
Any better way, other than goto or compiler specific solution?
This question was asked in comp.lang.c++ and the answers involved the use of
objects whose destructors are automatically called when getting out of
scope, however I was expecting suggestions that doesn't involve the use of
objects. So here is the question again.
I have a function like:
void fnc() {
char *mem1, *mem2, *mem3, *mem4;
FILE *fp1;
// lots of code...
// mem1, 2, 3, 4 got allocated
// lots of code and condition checks
if (condition_failed)
{
// blah blah
// free mem1, mem2, mem3, mem4
return;
}
if (condition2_failed)
{
// blah blah
// free mem1, mem2, ...
fclose(fp1);
return;
}
// here the end of routine (clean exit code):
// free mem1, mem2, mem3, mem4
}
Usually, I would use compiler specific solution by putting most of the code
in __try() and the clean exit code in _finally() block then to reach the
clean exit code I would invoke __leave.
Or use lables and then goto clean_exit
Any better way, other than goto or compiler specific solution?