R
Roubles
Hi All,
Here's my problem, I have a bunch of code that passes an allocated
object (say obj) to a function, and then dereferences that allocated
object when the function returns:
foo(obj);
obj->foobar = TRUE;
The issue is that foo *might* free obj. So the dereference can be
invalid.
I know the orthodox solutions to this problem are -
a) pass obj by reference and let foo set it to NULL.
b) have foo return a bool indicating that obj was free'd
I can't employ any of the orthodox solutions because there is too much
base code (foo comes from a set of a *huge* number of functions).
I am thinking of something like this:
int destroyed;
destroyed = 0;
obj->destroyed = &destroyed;
foo(obj); //obj has its own free routine, in which we can check
//to see if obj->destoyed has an address populated in
//it, and if it does we set its value to 1.
obj->destroyed = NULL;
if (!destroyed) {
obj->foobar = TRUE;
}
Basically, its like passing an int by reference to foo. The only
difference is that the reference is passed within an object. Does
anyone see anything wrong with the above ? I am wondering because I am
never seen this done before.
Or does anyone have any other suggestions ?
Thanks in advance,
roubles
Here's my problem, I have a bunch of code that passes an allocated
object (say obj) to a function, and then dereferences that allocated
object when the function returns:
foo(obj);
obj->foobar = TRUE;
The issue is that foo *might* free obj. So the dereference can be
invalid.
I know the orthodox solutions to this problem are -
a) pass obj by reference and let foo set it to NULL.
b) have foo return a bool indicating that obj was free'd
I can't employ any of the orthodox solutions because there is too much
base code (foo comes from a set of a *huge* number of functions).
I am thinking of something like this:
int destroyed;
destroyed = 0;
obj->destroyed = &destroyed;
foo(obj); //obj has its own free routine, in which we can check
//to see if obj->destoyed has an address populated in
//it, and if it does we set its value to 1.
obj->destroyed = NULL;
if (!destroyed) {
obj->foobar = TRUE;
}
Basically, its like passing an int by reference to foo. The only
difference is that the reference is passed within an object. Does
anyone see anything wrong with the above ? I am wondering because I am
never seen this done before.
Or does anyone have any other suggestions ?
Thanks in advance,
roubles