Memory troubles
Brought to you by:
macdome
OpenAG X does some bad things with memory.
This is known. Things seem to have been ok
yesterday, but they are now broken again, and I
can't get it to run inside Malloc Debug correctly.
1.1 will be released with these known memory
troubles. We will work from there. Expect a 1.1.1
soon.
Logged In: YES
user_id=151346
This should include replacing all calls to delete with
calls to a zap function such as:
// Put an assert to check if x is NULL, this is to catch
// program "logic" errors early. Even though delete
works
// fine with NULL by using assert you are actually
catching
// "bad code" very early
// Defining Zap using templates
// Use zap instead of delete as this will be very clean
template <class T>
inline void zap(T & x)
{
{assert(x != NULL);}
delete x;
x = NULL;
}
// In C++ the reason there are 2 forms of the delete
operator is - because
// there is no way for C++ to tell the difference between
a pointer to
// an object and a pointer to an array of objects. The
delete operator
// relies on the programmer using "[]" to tell the two
apart.
// Hence, we need to define zaparr function below.
// To delete array of pointers
template <class T>
inline void zaparr(T & x)
{
{assert(x != NULL);}
delete [] x;
x = NULL;
}
Logged In: YES
user_id=512169
Writing zap as a template is unnecessary.
just void zap(void*) will do the trick.
The standard says the delete(0) should be ok anyway.
This is potentially misleading and dangerous
since people may decide to use it to 'zap' memory
allocated with one of the [mc(re)]alloc routines.
Which should not be being used anyway. But still.
If you're really concerned about checking for NULL
then the optimum choice is provide your own global delete
and delete[] methods which do this checking.
This is allowed.
see http://ou800doc.caldera.com/HDK_basics/cplus_newdel.html
for a more detailed explanation.