PocketMatrix forums

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

Error unallocating mem!

Hi all, interesting problem;

I have a program working fine, I definded a pointer as thus:
Code:

unsigned short* all_zone_items_p = new PIXEL[300 * 120]; //buffer for all zone graphics.

and I can then use it fine etc....

but upon exiting the program I do this (including a few others as well), but this one seems to cause an illegal op, remarking it out works, but im unsure if the OS will automatically re-alocate the mem, if I don't, beside it's bad programming, any ideas why it may be causing an illigal op

Code:

delete [] all_zone_items_p;
man, you know you've been coding to long when you start ending staements with ;'s (see above)
;-)
BurningSheep · 02 Jul 2002, 10:05 UTC
offtopic but it looks kinda strange using unsigned short and PIXEL, why not do:
PIXEL* all_zone_items_p = new PIXEL[300 * 120];

neways, new should return a null pointer when unable to allocate memory, probably is a good habit to check that especially when allocating larger portions of memory (I usually still use malloc and free).
But your program would probably crash much earlier when you're trying to use a null pointer and I don't believe delete would do anything when given a null pointer.

besides this I don't see any errors in the code, so why it's not working... i don't know
I'm sure there's a reason im using unsigned short instead or pixel, but im not sure why.....mmm then again maybe not!.... maybe it's because the reference in easyce to the "real screen" location was defined as a US, I just copied it... No idea really!

in anycase does not fix problem!
Either debug, or stick a diagnostic message at allocation and deallocation, and report the memory address of the array. See if you are deallocating the same address as returned by new.

Dan East
angedelamort · 02 Jul 2002, 15:15 UTC
I had exactly the same problem. I was allocating in the constructor and deallocationg the array in the destructor... Even when I debugged, everything was correct. So, For now I just let the OS delete it for me lol. But, maybe in your case it's because you try to delete it 2 times?

Have fun debugging :)
Sounds like a heap corruption to me, the worst kind of bug. If you don't find it now, it'll crop up later on. It's not a good idea to ignore it.
Sorry, not illegal op, "Fatal Application Error"

Exception: 0x0000005
address: blah
Fatal Application Error
Hi,

It's 7:40am and I am not at my best but here are my first (and only other than coffee) thoughts:-

Dan's suggestion is absolutely correct, as you'd expect. But failing that try 'delete'ing immediately after the 'new' just to confirm in you own mind that the delete will work and does not always cause an exception.

Then move the new 'delete' further away from the 'new'. This is definitely the trial and error method of fixing heap corruption but it does give results - eventually.

Also, I am assuming this is a game, have you got the main game loop in a _try _catch block within WinMain?

Code:

__try
{
if (PeekMessage(&msg;, NULL, 0, 0, PM_REMOVE))
{
if( msg.message == WM_QUIT )
break;

TranslateMessage(&msg;);
DispatchMessage(&msg;);
}
else
{
if (!GameLib()->GetHibernate())
{
GameEvent();
}
}
}
__except(EXCEPTION_EXECUTE_HANDLER)
{
MessageBox(NULL, _T("Exception error!"), _T("Error"), MB_OK | MB_ICONERROR);
}
}




The origination of this code is from Jonathan Harbour's book and is probably the most useful piece of code in there.

I hope this helps as I really need that coffee now!
BadBazza
Whilst we are on the topic.....

Can anyone see anything wrong with the following as I seemed to of gained a memory leak and am having problems tracing it (the names of the variables have been changed to protect the innocent!)

Code:

BYTE *buffer;
buffer = new BYTE[17];
delete [] buffer;


There is other code in there, but the allocation and deletion is my main concern here

Thanks in advance
Bad
That looks text-book correct to me.

Dan East
Thanks Dan,

I'll continue digging

Cheers
Bad
i have a similar problem.

i do

Code:

unsigned short * single_frame = (unsigned short*)malloc( w*iFrameHeight*sizeof(short) );


and later on i do

Code:

#ifndef _WIN32_WCE_EMULATION
free( single_frame );
#endif


yes, you read right, if i allow the free() in the emulator, it crashes. But it works on the device. Any ideas?

cryo
Whenever I encounter a bizarre bug it is usually the result of one of two things; out of stack space, or memory is being written to out of bounds. The latter usually occurs by writing past the end of an array such as a string, etc. Such a bug will manifest differently on different hardware platforms, and is one of the leading causes that an application "does not work" on one platform while it works properly on another. The simple fact is that while the bug exists in all versions, it does not cause a catastrophic failure on the platforms in which it appears to work properly.

The best thing to do is step through the code carefully, while watching the variables and memory involved. If the memory watch window shows all question marks, then your application does not have access to that memory page and an access violation will occur. You may also have to set a debug breakpoint on a particular variable or memory address range to see what part of your app is writing to that memory.

The important thing is to not write this off as a problem with the emulator. More than likely there is a bug in your software, but at the moment it only manifests in the emulator.

Dan East
Are you sure that the memory from the malloc call was actually allocated? I've had things die on me when I accidentally tried to release a null pointer before - sometimes it works, sometimes it doesn't.

That doesn't explain why the deletion of the array isn't working though.
I have thus fixed the problem, the problem was I was trying to delete the "root" pointer in easyce that points to the "real screen" that the update() function uses, I've no idea why it "stuffs up", since its the last thing that my program does (i.e removes)...., maybe easyce is trying to delete it as well upon shutdown?,

anyhow the pointer thus:

pixel* screen_buffer_p;
screen_buffer_p = (PIXEL*) getbuffer();

can not be deleted at the end of the code.... why, no idea!