Archived

This topic is now archived and is closed to further replies.

Brennon

Memory still allocated!

Recommended Posts

Can anyone help me with this please!!! I am using vc++ 6.0. I have gone through every instance of an allocation of a pointer and i am sure that i have deleted it and set it to null on my game exit, yet I still get this error. Its getting bad especially if i get up to around level 10 of my game. Does anyone know of an application that could tell me what pointers are still allocated or something along these lines. Cheers in advance for any help.

Share this post


Link to post
Share on other sites
Thanks for the link on the smart pointers.

The thing I have noticed is that it says DIRECT3D (ERROR)

If this is the case then how can i see what direct x pointers are not released.

I am sure that I have released everything, but I will check again to make sure.

Now this will be a real newbie question... If I dont use the * to declare the pointer instance as in

TestClass * TestInstance;

but just use

TestClass TestInstance;

do I still delete it in the same way?



Share this post


Link to post
Share on other sites
quote:
Original post by Brennon
Now this will be a real newbie question... If I dont use the * to declare the pointer instance as in

TestClass * TestInstance;

but just use

TestClass TestInstance;

do I still delete it in the same way?
No. In fact, you cannot. That is a local variable, and is destroyed automagically when it exists the scope it was declared in (most commonly a function).

Share this post


Link to post
Share on other sites
Yeah, as CWizzard said. If you don''t declare it a pointer, it has memory allocated/deallocated for it automatically. In other words, you don''t need to use new to allocate memory for non-pointer variables. If you wish to use dynamic allocation (new)... you may do this allocation at any time (before you intend to use the variable), and subsequenctly delete it at the end of the scope. Remember, auto local variables are destroyed a the end of the scope, so you MUST NEVER return a pointer to them. You can however return a pointer to a static local variable, as it will remain valid/allocated within the global scope (but be accessible by name, only in the local scope). If you really need to be passing pointers around, and sharing and allocating/deallocating, I advice that you look at boost for a VERY useful set of headers, including special shared, owned, and reference counted (automatically deallocating) pointers. Boost is a very good set of headers to have in any case btw... and if you worry that they are non-standard, they are the probably the closest to an unofficial STL as you can get (and in the next C++ standard, I am sure many of their constructs will be promoted to offical STL status).

Share this post


Link to post
Share on other sites
another tip:

overload the "new" and "Delete" operators to monitor the number of allocs/deallocs to prevent leaks.

here you go:



#include "stdafx.h"

#include "mem.h"
#include "log.h"

int numBytesAllocated = 0;

int numAllocs = 0;
int numDeallocs = 0;

inline void *__cdecl operator new(size_t size)
{
numBytesAllocated += size;
void* res = malloc(size);
if (!res)
{
lprintf("ERROR: Cannot allocatem %d bytes of memory\n", size);
MEMORYSTATUS mem;
GlobalMemoryStatus(&mem);
lprintf(" Windows - %d alloc, %dk free of %dk total \n", mem.dwMemoryLoad, mem.dwAvailPhys >> 10, mem.dwTotalPhys >> 10);
lprintf(" Engine - %d alloc\n", numBytesAllocated);
}
numAllocs++;
return res;
}

inline void __cdecl operator delete(void * p)
{
if (p)
{
numDeallocs++;
free(p);
}
}


you''re welcome

[ My Site ]
All your source are belong to us
/*ilici*/

Share this post


Link to post
Share on other sites