   Corking Memory Heap leaks?
  Ok so i'm deleteing all my classes through my destructor using a little macro:#define SafeDelete(pObject) if(pObject != NULL) {delete pObject; pObject = NULL;}Now this works fine and i've used it on many other class pointers before (created using 'new')Now for some reason i get an unhandled exception in dbgheap.c when i call the SafeDelete function on my pBoard class pointer from the class is belongs to.I've checked through the debugger and the pointer being handed into dbgheap.c has the same value as the pointer for pBoard when its first created 0x00b62db0.So its a valid pointer yes? or perhaps no it seems.pBoard is not being double deleted and i've read somewhere it could be a memory heap leak which sorta makes sense since the rem statements in dbgheap.c where the program flags the unhandled exception says: /*    * Go through the heap regions and see if the pointer lies within one    * of the regions of the local heap.*/I'm not sure what i need to do from here so any help would be great, a learning experience is around the corner on this one i hope.   
  oh something which could be of use, within the pBoard's deconstructor i've used the macro below to delete two arrays of classes:#define SafeDeleteArray(p) { if(p) { delete[] (p); (p)=NULL; } }Its the only place i've used the macro across all my classes and this is the class thats flagging the problem so perhaps this has something to do with the memory problems, if it is a memory problem.   ;  - deleting a null pointer is completely safe, there's no reason to check if it's 0 before calling delete on it- is your object on the stack? has it already been deleted through another pointer?;Quote:Original post by Marx2052oh something which could be of use, within the pBoard's deconstructor i've used the macro below to delete two arrays of classes:#define SafeDeleteArray(p) { if(p) { delete[] (p); (p)=NULL; } }Its the only place i've used the macro across all my classes and this is the class thats flagging the problem so perhaps this has something to do with the memory problems, if it is a memory problem.was that array of objects created using new[] ??You're much better off using the standard containers and smart pointers for better resource management - virtually everything becomes automatic, and you don't have to spend productivity trying to track down things like this.   ;  I'm not an expert on memory/heap issues, but you might consider making a move towards the more 'modern' (read: easier to code, easier to debug, and more stable and robust) memory management methods available to C++ programmers.It looks to me like although you're programming in C++, you're using a number of C idioms (macros instead of functions, raw pointers, etc.).First of all, I'll point out that (AFAIK at least) it's perfectly safe to invoke delete on a null pointer in C++, so that part of the 'safe delete' macro is unnecessary. Second, consistent and proper application of RAII ('resource acquisition is initialization') in C++ programs can all but eliminate manual memory management via raw pointers from your code (which in turn will almost certainly eliminate errors like the one you're getting).The standard tools for RAII in C++ are the containers and other class templates from the standard library, and the smart pointer class templates from the Boost library (some of which will soon be part of the standard library itself). If you're used to C programming techniques and have a large code base built up already, making these changes might take a little work, but it will almost certainly pay off in the long run.   ;  What is the exact error message? More code would be useful too.   ;  Use the debugger to figure out what line causes the crash. Also, can you post some of the source code?   ;  Several reasons:- You have a memory overrun somewhere in your code ( why are you doing delete[]? There's a vector class for dynamic arrays), which is most likely the cause of the error- You have a dangling pointer reference somewhere (you point to an allocated object in two places, when you delete the object, the other reference points to an invalid location- You do not initialize pointers before allocating them, leaving the NULL condition worthless, since unallocated pointer will be pointing to garbage   ;  as mentioned by RDragon1 and jyk, this:if(ptr != null){  delete ptr;  ptr = null;}is 100% equal to this:delete ptr;ptr = null;deleting a null pointer does absolutely nothing (good or bad).  The C++ memory subsystem uses the 0 address as a sentinel value and will not do anything bad with it.the ONLY reason to have such a piece of code would be if you put in in a function in debug mode and set a break point to it to help find out if you are trying to delete pointers that are already null (which is sometimes a sign of mismanaging your object lifetime, but not always).The thing that breaks programs is deleting NON NULL pointers that you shouldn't be.  Such as pointers that reference memory that others still need, double deleting a pointer (very bad), addresses that we're not allocated with new, and addresses allocated with special versions of new, such as new[].new must be deleted with delete, new[] must be deleted with delete[] and malloc must be deleted with free. no mix and match allowed   ;  Right a loads of reponses here I appriceate it a lot.I'll try to list some points as answer to your questions:1:- No i haven't been using 'new[]' but a for statement containing 'new' to init my array of objects (possibly the cause of the problem)2:- All my points are NULL valued before use3:- I've been using those macros since i learned myself C++ so i guess its just something thats always seemed to work ok for me so i haven't needed to change it, but i'm always ready to learn a better way4:- The object hasn't been deleted elsewhere, only one pointer of it exists and its no referenced anywhere in the project.I've never tried to use the <vector> classes I don't really know what they do so i've never been drawn to try and learn them.I've never used new[] when working with arrays of objects it makes complete sense to use it though i think i've just never learned that bit, i'll look into it now and see if using it corrects the issue.   