Guarding delete
-
Does anyone know of a way to guard a delete operator from trying to release a non-allocated block, as in the following trivial example. short x[ 100 ]; try { delete []x; } catch( ... ) { return MDERROR; } This does not work as the exception is caught somewhere in the bowels of the system and I get either an assert or a user breakpoint box. I have looked at RTTI but it doesn't help me. How can I prevent a crash if I'm passed a pointer to a global or static block? Thanks all!
-
Does anyone know of a way to guard a delete operator from trying to release a non-allocated block, as in the following trivial example. short x[ 100 ]; try { delete []x; } catch( ... ) { return MDERROR; } This does not work as the exception is caught somewhere in the bowels of the system and I get either an assert or a user breakpoint box. I have looked at RTTI but it doesn't help me. How can I prevent a crash if I'm passed a pointer to a global or static block? Thanks all!
This function determines whether a given pointer belongs in the stack or not:
bool is_stack_memory(void * p)
{
char check;
MEMORY_BASIC_INFORMATION m1,m2;
::ZeroMemory(&m1,sizeof(m1));
::ZeroMemory(&m2,sizeof(m2));
::VirtualQuery(p,&m1,sizeof(m1));
::VirtualQuery(&check,&m2,sizeof(m2));
return m1.BaseAddress==m2.BaseAddress;
}So you can write code like
if(is_stack_memory(x)){
printf("x has not been dynamically allocated\n");
return MDERROR; // or whatever
}
else{
delete [] x;
}Anyway, IMHO designing functions around this feature is not a very good idea. I'd restrict the ability to determine whether a pointer is
delete
able to doing error checking in debug mode. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo PS: There's a limitation inis_stack_memory
, namely that it fails to identify pointers on other threads' stacks. This could be worked around, however. -
This function determines whether a given pointer belongs in the stack or not:
bool is_stack_memory(void * p)
{
char check;
MEMORY_BASIC_INFORMATION m1,m2;
::ZeroMemory(&m1,sizeof(m1));
::ZeroMemory(&m2,sizeof(m2));
::VirtualQuery(p,&m1,sizeof(m1));
::VirtualQuery(&check,&m2,sizeof(m2));
return m1.BaseAddress==m2.BaseAddress;
}So you can write code like
if(is_stack_memory(x)){
printf("x has not been dynamically allocated\n");
return MDERROR; // or whatever
}
else{
delete [] x;
}Anyway, IMHO designing functions around this feature is not a very good idea. I'd restrict the ability to determine whether a pointer is
delete
able to doing error checking in debug mode. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo PS: There's a limitation inis_stack_memory
, namely that it fails to identify pointers on other threads' stacks. This could be worked around, however.or, if you ALWAYS init pointers to NULL, and ALWAYS reset them to NULL after delete, like I do, then:
if (pointer)
delete [] pointer;
pointer = NULL;Sorry to dissapoint you all with my lack of a witty or poignant signature.
-
Does anyone know of a way to guard a delete operator from trying to release a non-allocated block, as in the following trivial example. short x[ 100 ]; try { delete []x; } catch( ... ) { return MDERROR; } This does not work as the exception is caught somewhere in the bowels of the system and I get either an assert or a user breakpoint box. I have looked at RTTI but it doesn't help me. How can I prevent a crash if I'm passed a pointer to a global or static block? Thanks all!
If I were you, I would review my design. By this I mean that an object shouldn't deallocate memory it didn't allocate (which is obviously the case here since your object can't figure out if it is stack or heap memory), unless you have a good reason. If I am wrong or said something stupid, I apologize in advance ;) Michel
-
Does anyone know of a way to guard a delete operator from trying to release a non-allocated block, as in the following trivial example. short x[ 100 ]; try { delete []x; } catch( ... ) { return MDERROR; } This does not work as the exception is caught somewhere in the bowels of the system and I get either an assert or a user breakpoint box. I have looked at RTTI but it doesn't help me. How can I prevent a crash if I'm passed a pointer to a global or static block? Thanks all!
Thanks for your thoughts. I didn't design or write this stuff. I'm only trying to wrap an audio interface I got pre-cooked. I watch my own new's and delete's like a hawk and always have the leakage detector on. The bit about nulling the pointer is very good (simple and obvious) and will be standard in my code from now on. I do however find it strange that delete doesn't throw a catchable exception. There must be a good reason for it. I will try it on Linux and see if it tells me why.
-
Thanks for your thoughts. I didn't design or write this stuff. I'm only trying to wrap an audio interface I got pre-cooked. I watch my own new's and delete's like a hawk and always have the leakage detector on. The bit about nulling the pointer is very good (simple and obvious) and will be standard in my code from now on. I do however find it strange that delete doesn't throw a catchable exception. There must be a good reason for it. I will try it on Linux and see if it tells me why.