473,322 Members | 1,911 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Is there anything wrong with the following ?

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
Nov 14 '05 #1
3 1354
"Roubles" <ro*******@hotmail.com> wrote in message
news:87**************************@posting.google.c om...
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) {
Here, you might want to:

obj->destroyed = 0;

Just in case you forget to set it to somewhere valid and create a
hard-to-spot bug.
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.
It'll certainly work.
Or does anyone have any other suggestions ?


You could use wrapper functions, that manage the was-it-destroyed process
and call down to the real function. Tedious if there are a lot of functions,
but would make the calling code clearer IMHO. Perhaps you could generate the
code with a small program.

If the functions all have the same parameter and return types, or there are
a limited number of variants, you could write function(s) taking a function
pointer to get almost the same effect as above with less effort.

A more radical alternative would be to keep track of what objects exist or
have been destroyed yourself, allowing you to validate a pointer.

Alex
Nov 14 '05 #2
Roubles wrote:
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.
If foo() can free() an object in which the rest of the
program is still interested, there's something fundamentally
wrong with either foo() or the rest of the program.
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).
Sounds like a very peculiar program. Are you trying to
imitate a garbage collector, perhaps?
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;
No good: If `obj' has already been free()d, `destroyed'
has the same problem that you're trying to solve for `foobar'.
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.
It looks like it should work (once you move the reference
to `obj->destroyed' inside the `if').

As for why you've never seen it -- well, I've never seen
anything quite like it, either. What's more, I hope never to
see it again! It looks error-prone as all-get-out: Imagine
what would happen if somebody forgets to clear `obj->destroyed'
after just one of the "*huge*" number of foo() calls. It's a
wart on a blemish on a blister, and I say "A pox upon it!"
Or does anyone have any other suggestions ?


The management of dynamic memory requires discipline. One
way to obtain the necessary discipline without too much pain is
to impose some kind of structure on the proceedings. Two common
patterns (which can sometimes be intermingled) are:

- Localize all the allocating and freeing to a few well-
controlled places. Variation: manage the memory for each
object type in a pair of type-specific functions, rather
like constructors and destructors in some other languages.

- Stick a common memory-management header on every object,
and use this header to do whatever magic you require:
reference counting, garbage collection, et cetera. If
you formulate (and obey) simple rules about what does
and doesn't make an object eligible for being freed, you
won't need to ask "Is it still there?" questions.

What you *don't* want to do is just have every function in
the program feeling like it has the privilege to destroy any
object it wants to, any time it wants to. That is not discipline;
that's anarchy.

I'm afraid these comments are too vague to help you toward
an immediate concrete implementation -- but since you haven't
described what's going on with all these uncivilized functions,
I can't be very specific.

--
Er*********@sun.com

Nov 14 '05 #3
"Roubles" <ro*******@hotmail.com> wrote in message

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.

You've got a problem already. If foo() frees obj it should at least return a
value to say that it has done so. Better still if foo() is rewritten so it
doesn't free anything and obj is freed in a destructor.
If you can't rewrite foo() then there is no good solution.
Nov 14 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: John Black | last post by:
Hi, I have some code like this, itr = find_if(this->pool.begin(), this->pool.end(), bind2nd(mem_fun_ref(&Pool::isAvailable), make_pair(base, high))); But compiler always complains a lot STL...
4
by: Chris | last post by:
Could anyone please help me. IS there something wrong with it? char * function getString() { char myString; sprintf(myString, "hello world");
4
by: Morten Overgaard | last post by:
Hi I'm listening on the SysLog port (514) through UDP. The problem is that I am not receiving anything nut I know that i get messages on the port. When I use KIWI to listen on the same port via...
1
by: D. Bron | last post by:
Hello, Can anyone tell me why I'm getting the following exception: Method not found: Void Maple.Collections.Set.UnionAll(System.Collections.ICollection) I'm not using reflection, I'm not...
138
by: Ian Boyd | last post by:
i've been thrown into a pit with DB2 and have to start writing things such as tables, indexes, stored procedures, triggers, etc. The online reference is only so helpful. The two pdf manuals are...
83
by: Anonymous | last post by:
Came across some code summarized as follows: char const* MyClass::errToText(int err) const { switch (err) { case 0: return "No error"; case 1: return "Not enough"; case 2: return "Too...
1
by: Landes | last post by:
Hiya, I have the following code in a C# aspx page, but I keep getting the error that 'ExceptionCategoryId_' not found. Looks like the QueryString is not returning anything. <asp:DataGrid...
28
by: hijkl | last post by:
hey guys anything wrong with this code?? if it is then what? int *array(int n){ return new int(n); } int main(){ int *p = array(10); for( int i = 0; i < 10; i++ ) {
10
by: Jim Langston | last post by:
I use a game engine using MSVC++ .net 2003 and have no problems. Some users of DevC++ who use the same engine crash at times when a copy of this structure is the return variable. I don't have...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.