Completely Deleting Pointers

M

Min

CFool
{
public:
void StartWorldWarIII() { /*** Launch Nuclear Missiles **/ }
};

int main()
{
CFool* pFool = new CFool();
delete pFool;
//////******* const unsigned int GARBAGE = 0xA3;
///// ******* memset( pFoo, GARBAGE , sizeof( CFool) );
pFool->StartWorldWarIII();

return 0;
}

Among other techniques from Writing Solid Code by Steve Maguire, I tried to
scramble the content by filling garbages ( the commented lines ). But it
does not guarantee that "pFoo->StartWorldWarIII()" will crash, but the
object will output garbage, the next time it is used. (an obvious attempt
to indicate that the instance has been deleted.)

Anyway, Anybody knows any trick or technique to guarantee
"pFoo->StartWorldWarIII()" will crash ? for debugging purpose.
(I have a feeling that I am asking it in a wrong NG, but what the heck.)

TIA
 
H

Haplo

CFool
{
public:
void StartWorldWarIII() { /*** Launch Nuclear Missiles **/ }
};

int main()
{
CFool* pFool = new CFool();
delete pFool;
//////******* const unsigned int GARBAGE = 0xA3;
///// ******* memset( pFoo, GARBAGE , sizeof( CFool) );
pFool->StartWorldWarIII();

return 0;
}

Among other techniques from Writing Solid Code by Steve Maguire, I tried to
scramble the content by filling garbages ( the commented lines ). But it
does not guarantee that "pFoo->StartWorldWarIII()" will crash, but the
object will output garbage, the next time it is used. (an obvious attempt
to indicate that the instance has been deleted.)

Anyway, Anybody knows any trick or technique to guarantee
"pFoo->StartWorldWarIII()" will crash ? for debugging purpose.
(I have a feeling that I am asking it in a wrong NG, but what the heck.)

How about

pFool = NULL;

after you delete it ?


Haplo
 
?

=?iso-8859-1?Q?Juli=E1n?= Albo

Min escribió:
int main()
{
CFool* pFool = new CFool();
delete pFool;
//////******* const unsigned int GARBAGE = 0xA3;
///// ******* memset( pFoo, GARBAGE , sizeof( CFool) );
pFool->StartWorldWarIII();

return 0;
}

Among other techniques from Writing Solid Code by Steve Maguire, I tried to
scramble the content by filling garbages ( the commented lines ). But it

I read that book, and it clearly explains that you must not use memory
you have freed. You must not put garbage after delete it.

Regards.
 
A

Andre Kostur

How about

pFool = NULL;

after you delete it ?

That doesn't guarantee a crash. That invokes "Undefined Behaviour",
which might include a crash (then again, it might do nothing. I know of
at least one implementation where the above code will function perfectly
fine, if StartWorldWarIII doesn't reference any class members....)

To the OP, you might want to implement your own smart pointer which will
call something like abort() or terminate() if one attempts to dereference
a NULL pointer.
 
M

Min

Sorry, there is more clarification.

delete pFool;
//////******* const unsigned int GARBAGE = 0xA3;
///// ******* memset( pFoo, GARBAGE , sizeof( CFool) );

should be read as
//////******* const unsigned int GARBAGE = 0xA3;
///// ******* memset( pFoo, GARBAGE , sizeof( CFool) );
delete pFool;

Of course, "pFool = NULL;" could partially solve the problem. But failure
to do so won't. Consider this:

//******** Code Begins ******************
CFool* pSaddam = new CFool();
CFool* pUday = pSaddam;

//////******* const unsigned int GARBAGE = 0xA3;
///// ******* memset( pSaddam, GARBAGE , sizeof( CFool) );
delete pSaddam;
pSaddam = NULL;

pUday->StartWorldWarIII();

//************************************
It is very obvious in this example. But what I am trying to accomplish is
than even forgetting to set to NULL, any pointers still pointing to an
invalid memory block will gurantee to crash, if it is used again after
deletion. By scrambling pSaddam before deletion, pUday might not do things
properly, but it is still doing something, instread of plain crashing.
 
?

=?iso-8859-1?Q?Juli=E1n?= Albo

Min escribió:
Sorry, there is more clarification.

delete pFool;
//////******* const unsigned int GARBAGE = 0xA3;
///// ******* memset( pFoo, GARBAGE , sizeof( CFool) );

should be read as
//////******* const unsigned int GARBAGE = 0xA3;
///// ******* memset( pFoo, GARBAGE , sizeof( CFool) );
delete pFool;

That way the destructor of the object pointed by pFool will probably
crash. I think the only safe mode to put garbage in the memory used by
the object is overloading operator new and operator delete.

Regards.
 
R

Ron Samuel Klatchko

One problem you have with this is once the memory is freed, that
memory is up for grabs. So even if you do something that 'guarantees'
a crash, once that memory is allocated again, the memory contents will
get changed.

So, the first thing to note is that if you want a true guarantee, you
can't actually free it. Now this means that you will have a memory
leak. If this is an object that gets allocated constantly, then you
are probably SOL. But if these objects are only created rarely, you
might be fine.

And also to note, this means that objects that are put on the stack
are beyond hope. Due to the way stacks work, there is no way to
prevent the memory from being freed.

So, with all those caveats, here is quickly thought out method. First
create
the base class death_on_delete:

class death_on_delete
{
public:
base() : deleted_(false) {}
~base() { deleted_ = true; }
void operator delete(void *) { /* do nothing */ }
void check() const { assert(deleted_ == false); }

private:
bool deleted_;
};

Any class that derives from this and is created on the heap will never
truly go away. Again, this does not work for classes on the stack
(nor for classes that override operator delete).

Then, in all your methods, you need to add a call to
death_on_delete::check(). This method will assert if it is called
after the function has been deleted.

Have fun.

samuel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top