Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » delete operator causing assertion

This thread is locked; no one can reply to it. rss feed Print
delete operator causing assertion
Neil Walker
Member #210
April 2000
avatar

This happens about 1 in 5 times i exit from my game. I'm getting an assertion from the microsoft library (delete operator method in the dbgdel.cpp file) and it's at this line:

_ASSERTE(_BLOCK_TYPE_IS_VALID(pHead->nBlockUse));

Anyone got a clue what it means! all i'm doing is deleting some memory, as in:

delete car;

Neil.

Neil.
MAME Cabinet Blog / AXL LIBRARY (a games framework) / AXL Documentation and Tutorial

wii:0356-1384-6687-2022, kart:3308-4806-6002. XBOX:chucklepie

Billybob
Member #3,136
January 2003

What's the assertion error? It should give you some cryptic assertion error you can good for. Like for me, I would get something like CtrlValidHeapPointer assertion error and Google told me it was about deleting a pointer in the wrong heap.

Neil Walker
Member #210
April 2000
avatar

hello,
error is _CrtIsValidHeapPointer(pUserData), line 1132 of dbgheap.c
/*

  • If this ASSERT fails, a bad pointer has been passed in. It may be

  • totally bogus, or it may have been allocated from another heap.

  • The pointer MUST come from the 'local' heap.

*/
_ASSERTE(_CrtIsValidHeapPointer(pUserData));

the line that my debugger stops me in is in the file dbgdel.cpp (delete operator) on the ASSERT line below:

void operator delete(
void *pUserData
)
{
_CrtMemBlockHeader * pHead;

RTCCALLBACK(_RTC_Free_hook, (pUserData, 0));

if (pUserData == NULL)
return;

_mlock(_HEAP_LOCK); /* block other threads */
__TRY

/* get a pointer to memory block header */
pHead = pHdr(pUserData);

/* verify block type */
_ASSERTE(_BLOCK_TYPE_IS_VALID(pHead->nBlockUse));

_free_dbg( pUserData, pHead->nBlockUse );

__FINALLY
_munlock(_HEAP_LOCK); /* release other threads */
__END_TRY_FINALLY

return;
}

this only happens now and again. when i debug the code where it fails the object i'm deleting is perfectly valid and i can inspect all its data.

Neil.
MAME Cabinet Blog / AXL LIBRARY (a games framework) / AXL Documentation and Tutorial

wii:0356-1384-6687-2022, kart:3308-4806-6002. XBOX:chucklepie

X-G
Member #856
December 2000
avatar

You're probably trying to delete a dangling or NULL pointer, or something that came from a DLL.

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

Neil Walker
Member #210
April 2000
avatar

i don't think so as when i debug the code all data is fine and the code isn't doing anything different i can see when it fails or when it works.

however, i've moved the delete statement (deleting some memory created inside a struct) to the destructor of the struct and it seems to behaving itself a bit better. though time will tell :)

Neil.

Neil.
MAME Cabinet Blog / AXL LIBRARY (a games framework) / AXL Documentation and Tutorial

wii:0356-1384-6687-2022, kart:3308-4806-6002. XBOX:chucklepie

X-G
Member #856
December 2000
avatar

Show code?

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

Billybob
Member #3,136
January 2003

Quote:

error is _CrtIsValidHeapPointer(pUserData)

Same error I got.
You are deleting a peice of memory that was allocated in another heap (in another DLL for example).

tobing
Member #5,213
November 2004
avatar

Maybe this will go away if you compile everything to use runtime-DLL (this has helped some people out).

gillius
Member #119
April 2000

A double delete (dangling pointer), a non-initialized pointer, or a pointer from a different heap will cause this assert. A NULL pointer does not cause it, as X-G conjectured, because deleting NULL pointers is safe and well-defined.

Gillius
Gillius's Programming -- https://gillius.org/

X-G
Member #856
December 2000
avatar

What he said. Sorry. I should know that.

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

Billybob
Member #3,136
January 2003

Quote:

because deleting NULL pointers is safe and well-defined.

Actually, deleting a NULL pointer under MSVC 6 will cause an assert. I've run into it, kind of annoying. Not that it's bad, or will crash in Release mode, but it certainly asserts.

gillius
Member #119
April 2000

Uh are you sure about that WH? I've long since deleted my copy of VC6, so I can't test it anymore, but I certainly don't remember its delete operator asserting on "delete 0;" and I used MSVC6 for a few years to do all of my C++ programming.

Gillius
Gillius's Programming -- https://gillius.org/

tobing
Member #5,213
November 2004
avatar

I'm also wondering about this 'delete 0' causes an assert. Never seen that, I'm using MSVC 6.0 7.0 7.1 for years now.

Tobias Dammers
Member #2,604
August 2002
avatar

If delete 0; raises an assert in MSVC, then it's even worse than I always thought. ASSERT(NULL) should of course assert, but delete NULL shouldn't.

---
Me make music: Triofobie
---
"We need Tobias and his awesome trombone, too." - Johan Halmén

Chris Katko
Member #1,881
January 2002
avatar

Quote:

I'm also wondering about this 'delete 0' causes an assert.

I'm confused. You can't write delete 0, or delete NULL. NULL (and '0') isn't a pointer. So I'm just going to assume you mean variable containing a null pointer.

If this:

int *temp = new int;
delete temp;
delete temp;

is what you're talking about, yes, it crashes (or gives an unhandled exception in debug mode) when using MSVC6.

Quote:

then it's even worse than I always thought.

How good do you expect a compiler to be that's made before the specification is standardized?

-----sig:
“Programs should be written for people to read, and only incidentally for machines to execute.” - Structure and Interpretation of Computer Programs
"Political Correctness is fascism disguised as manners" --George Carlin

gillius
Member #119
April 2000

Hmm 0 is implicitly convertable to any type of pointer, as guaranteed by the standard, so I thought delete 0; would work. In fact, GCC won't compile that, so I wonder. Casting it to int* or making "int* i=0; delete i;" does work.

Gillius
Gillius's Programming -- https://gillius.org/

Chris Katko
Member #1,881
January 2002
avatar

Quote:

Casting it to int* or making "int* i=0; delete i;" does work.

Yes, I guess I just misinterpreted what you were saying. :)

-----sig:
“Programs should be written for people to read, and only incidentally for machines to execute.” - Structure and Interpretation of Computer Programs
"Political Correctness is fascism disguised as manners" --George Carlin

gillius
Member #119
April 2000

No, you didn't misinterpret me. I literally meant "delete 0;" when I said that, but at the time I honestly thought it would work. On GCC at least, it doesn't.

Gillius
Gillius's Programming -- https://gillius.org/

Billybob
Member #3,136
January 2003

Man, I have bad memory ;) I had always recalled getting asserts on NULLs. A test program under MSVC 6 shows nothing. Ok, my bad.

Go to: