PocketMatrix forums

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

leak in PocketFrog

There's a leak in PocketFrog that shows up when the library is started and stopped more than once within the same instance of an application.

I've made a game based on PocketFrog. I've also made a level editor for the game. The level editor runs on Win32 and I'm using the amazing feature of PocketFrog that enables the same code to run both on PocketPC and Win32 to also run my game from inside the level editor. The idea is to let the player test the level he is working on without leaving the level editor or having to move the level file over to a PocketPC device.

Everything works the first time the game is started from the level editor. But if the user closes the game and then starts it again, the game fails to start. This is even though I instantiate the entire game on the stack and let it fall out of scope each time the user ends it.

It looks like Thierry is aware of the problem because I have found the following in the PocketFrog code:

<b>PocketPC.cpp</b>

Code:

DisplayDriver* l_displayDriver; //fixme: leak at exit
InputDriver* l_inputDriver; //fixme: leak at exit

Also, in the same file, there's an InitDrivers function that starts like this:

Code:

bool InitDrivers( HWND hwnd )
{
// Check if the drivers are already initialized
if (l_displayDriver && l_inputDriver)
return true;

The second time the game is started, the function returns immediately, as if the drivers are already initalized. But at this point the drivers have been released and should be initalized again.

However, I'm unable to fix this because the only place that I can find that l_displayDriver and l_inputDriver pointers are ever initalized is further down in the same function:

Code:

std::auto_ptr<DisplayDriver> display( new Win32Display );
std::auto_ptr<InputDriver> input( new Win32Input );

But of course, that's after the pointers have been tested so they have to be initalized somewhere else as well. They are both declared in an anon namespace.

Anyone know how to resolve this issue?

Thanks,

Roger Dahl
Yea, those leaks are known about ( http://pocketmatrix.com/forums/viewtopic.php?t=8281 )

If I remember correctly I fixed it for myself by adding two deletes in the ShutDown() function of the PocketFrog framework.
Thanks for helping me out. I inserted the deletes and that worked.

Now I'm working on removing memory leaks from my game. I'm having trouble fixing one that involves a rasterizer I've created with:

Code:

raster = display->CreateRasterizer(surface);

When I try to 'delete raster;', I get the following compiler error:


<PF>\src\foreign\boost\scoped_ptr.hpp(50) : error C2027: use of undefined type 'Frog::Internal::AlphaRaster2D'
<PF>\src\rasterizer.h(30) : see declaration of 'Frog::Internal::AlphaRaster2D'
<PF>\src\foreign\boost\scoped_ptr.hpp(49) : while compiling class-template member function 'Frog::scoped_ptr<T>::~scoped_ptr(void)'
with
[
T=Frog::Internal::AlphaRaster2D
]
<PF>\src\rasterizer.h(118) : see reference to class template instantiation 'Frog::scoped_ptr<T>' being compiled
with
[
T=Frog::Internal::AlphaRaster2D
]
<PF>\src\foreign\boost\scoped_ptr.hpp(50) : error C2466: cannot allocate an array of constant size 0
<PF>\src\foreign\boost\scoped_ptr.hpp(51) : warning C4150: deletion of pointer to incomplete type 'Frog::Internal::AlphaRaster2D'; no destructor called
<PF>\src\rasterizer.h(30) : see declaration of 'Frog::Internal::AlphaRaster2D'

Anyone know how to free a Rasterizer?

Thanks,

Roger Dahl
Do the comments in scoped_ptr.hpp help? They discuss deleting a scoped pointer using reset() ???
It looks like that to be able to delete a Rasterizer object, rasterizer.h must #include "internal\\raster2d.h". The reason is that the compiler can't compile ~scoped_ptr for a class without a complete declaration of that class.

Roger
Regarding the l_displayDriver and l_inputDriver issue:

When I cusomized PF to work as a subwindow (using PF as a GDI wrapper) I had to insert a function into the pocketpc.h/cpp file that reset those pointers to NULL (or ZERO). There arn't actually a leak, since they are scoped pointers, but they are never reset to zero when PF exits. So, before you launch a PF window again (from within a PF session) you need to reset those pointers.

I have no experience with freeing a rasterizer, but I think Conans post pretty much nails it down. You can't use regular delete on a scoped pointer. You got to use reset() (just look at some of the code Thierry wrote and you'll figure it out).
Sorry about posting as anon. There's a bug on the site and I'm not able to log in.

There is an actual memory leak. The reason is that l_displayDriver and l_inputDriver are plain pointers. Take a look at the code:

Code:

DisplayDriver* l_displayDriver; //fixme: leak at exit
InputDriver* l_inputDriver; //fixme: leak at exit
<snip>
// Try Win32 display
std::auto_ptr<DisplayDriver> display( new Win32Display );
std::auto_ptr<InputDriver> input( new Win32Input );

if (display->Initialize( hwnd ) && input->Initialize( hwnd ))
{
l_displayDriver = display.release();
l_inputDriver = input.release();
return true;
}


The auto pointers only exist during the call to Initalize(), the advantage being that there is no memory leak if Initalize() throws.

A Rasterizer* is also a plain pointer and the object must 'delete'ed. The reason there's trouble with a scoped_ptr is that the Rasterizer class itself holds other pointers in scoped_ptrs.

Roger
Roger wrote:There is an actual memory leak. The reason is that l_displayDriver and l_inputDriver are plain pointers.

Right... my bad! I seemed to remember that those objects where reclaimed and destroyed elsewhere however, and then the l_displayDriver and l_inputDriver is simply two pointers that should NOT be deleted. But I can't seem to locate that piece of code so I guess you're right. (Hmm.. perhapps it turned up when I was debugging..)

Anyways.. As LostGoat posted earlier: They should probably be deleted before exit or before trying to initialize them a second time.