PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

CGapiSurface destrucor works strange

Hi!

I've got a strange problem. I have a vector of CGapiSurfaces. I have problems destroying it.
When I run this code:
Code:

std::vector<CGapiSurface*> surfaces;
CGapiFileStream* stream = new CGapiFileStream();
HRESULT result = GD_OK;
TCHAR* file = new TCHAR[255];
for (DWORD i = 0 ; i < phases; i++)
{
CGapiSurface* surface = new CGapiSurface(m_pGapiDraw);
_strintf(file, fileName, i + 1);
result = stream->Open(vfs, file);
result =surface->CreateSurface(GDSURFACE_SYSTEMMEMORY, stream);
surfaces.push_back(surface);
stream->Close();
delete surfaces[i];
}

It works as expected: I have the same amount of memory after this than before. (I traced and watch it
by task manager and process explorer)
BUT if I leave out the delete surfaces[i] row (13), and rigth after the loop I run a second one:
Code:

for ( i = 0 ; i < phases; i++)
delete surfaces[i]

the memory is not freed (or a few bytes only)
If I create once more the surfaces, it does not
allocate extra memory for it, it uses the not-freed memory.
I also tried surfaces[i]->FreeSurface(), and surfaces[i]->CreateSurface(GDSURFACE_SYSTEMMEMORY, DWORD(0), DWORD(0)) before deleting it, but they didn't help.


I tried with a static array as well, but the result is the same.
What can I do now?
Please, help :)

Thanks
Pd3a_dot_com · 11 Jan 2006, 12:54 UTC
And we are using Gapidraw 3.51c, the latest.
(im a collegue of Latz)
I forget to mention that I met this problem on both PC and PPC.
InexorableTash · 12 Jan 2006, 18:15 UTC
Task Manager/Process Explorer are ineffective ways of looking for leaks.

What's probably happening is that in the first case your heap is only growing by the size of a single surface. You use a surface then immediately delete it, so the next allocation can re-use the space used by the previous surface. So your heap never grows past (base+1*surface).

In the second case, you allocate all of the surfaces before deleting any, so each one takes up new space in the heap. Before you delete anything, your heap has grown to (base+n*surface).

Now just because you release something doesn't mean your heap shrinks. "heap" is a concept internal to a process, so the OS only knows that the app has asked for a certain amount of memory; it doesn't necessarily know that it has temporarily stopped using it - which is (usually) all that a "delete" does.

Those tools (process explorer, task manager) usually measure something called "memory usage", which is how much memory the process has asked the OS for. This generally doesn't go down. What does go down is something called "working set" which is the amount of memory that the process is touching on a regular basis. This includes both code and data - if you had an infinite loop (10 GOTO 10) your working set would "shrink" to just the page of memory the instruction was on. If you zero out a gigabyte of memory, your working set will temporarily spike up to a gigabyte, then drop back down, with lots of disk thrashing in the mean time.

Note that as long as an application isn't using memory that it's asked for (e.g. it allocated a gigabyte, then freed it but the heap is stil a gig), the OS doesn't bat an eye; per-process memory is (mostly) "free" in a protected memory environment. Each process can ask for 2 GB and the system doesn't even blink. What slows a proces down is a large working set, since the OS needs to actually move those pages from disk into physical memory. But if the working set is small no paging is necessary so things are fast.

...

For leak checking I use the CRT Debug Heap functions.

http://msdn.microsoft.com/library/defau ... _c2b2b.asp

Pay attention to "The Debug Heap from C++". You simply redefine new and delete operators, then just before your process exits you call _CrtDumpMemoryLeaks which reports any leaks.

Works like a charm!
Thanks for this detailed help, I will try.
hammet84 · 04 Mar 2009, 09:26 UTC — I want to know the solution!
So.. finally, what's the solution of this problem? because for me is the same in my code, exactly the same lines and I tryed the same with static arrays... I have memory leaks with the surfaces after delete them... anybody can help me? I would be very pleased...

I'm living an inferno ;(