Skip to main content
gamedev.net
🔒 Locked

Problems deleting dynamically allocated memory..

Started by Alex Swinney 12 replies 1.8k views
Original Post
Alex Swinney
Alex Swinney
This is really embarassing..somehow my program (VS.NET) isn't allowing me to delete dynamically allocated memory. I'm sure I'm missing something..but here's what I've got:

	ter = new TerrainVertex[desc.Size/sizeof(TerrainVertex)+1];
	ter2 = new TerrainVertex[desc.Size/sizeof(TerrainVertex)+1];

	delete [] ter;
	delete [] ter2;

That is all in one function, and this function is called once. 'ter' and 'ter2' are both of type TerrainVertex, which is defined by my program. I had my destructor with this as it's contents: delete [] this; but that still didn't delete the memory..what should go in there? It is giving me this assertion error (first, there are others that follow): _BLOCK_TYPE_IS_VALID(pType->nBlockUse)
I WISH SOMEONE WOULD FIX THE DAMNED LOGIN!
VolkerG
VolkerG
May it be that the code between these lines demages the allocated blocks?
SiCrane
SiCrane
Don't call delete this in a destructor. Freeing the memory is the responsibility of whatever invoked the destructor, not the destructor. Only delete objects that the class has ownership of; ex: buffers it allocated itself.
Alex Swinney
Alex Swinney
Quote:
Original post by VolkerG
May it be that the code between these lines demages the allocated blocks?


The code between the new and delete uses the two variables to read one DX vertex buffer and write to another DX vertex buffer.

Isn't it a little asinine to have to have an empty destructor? If I remove the destructor entirely, I get an error, but it has to be empty or I get an error..
I WISH SOMEONE WOULD FIX THE DAMNED LOGIN!
snk_kid
snk_kid
Quote:
Original post by Alex Swinney
Isn't it a little asinine to have to have an empty destructor? If I remove the destructor entirely, I get an error, but it has to be empty or I get an error..


if your destructor doesn't have anything to do then don't declare & define one, one will be implicitly defined.

you only need to explicitly define a destructor to release resources most commonly when a class acquires a resource itselfs usually in the constructor e.g.

struct foo {  int* ptr;  foo(int n): ptr(new [n]) {} //aquire resource  ~foo() { delete[] ptr; } //release resource};
OrenGL
OrenGL
If you make an independent 5 line program and run these lines does it work? If not have a look into the TerrainVertex destructor if it does work the problem is not these lines but in something else.
Don't shoot! I'm with the science team.....
lemurion
lemurion
Quote:
Original post by Alex Swinney
'ter' and 'ter2' are both of type TerrainVertex

shouldn't they be of type TerrainVertex * ??

hope that helps !
Matt
Matt
Alex Swinney
Alex Swinney
Quote:
Original post by lemurion
Quote:
Original post by Alex Swinney
'ter' and 'ter2' are both of type TerrainVertex

shouldn't they be of type TerrainVertex * ??

hope that helps !
Matt


Sorry, they are, brainfart.
I WISH SOMEONE WOULD FIX THE DAMNED LOGIN!
Alex Swinney
Alex Swinney
Ok, does anyone know DirectX?

I removed the lines where I declared the size of the array, and let them be determined by the vertex buffer lock function. I also removed the delete [] calls. I do not get an unfreed memory error from DXDebug..so I am wondering if lock allocates and deletes?

[EDIT] Nevermind, I get an unfreed memory error upon exit..

So, if I remove the delete calls, it works, but memory is unfreed and if I leave them in there I get this crap:
_BLOCK_TYPE_IS_VALID(pType->nBlockUse)

Ugh..
I WISH SOMEONE WOULD FIX THE DAMNED LOGIN!
Agony
Agony
The pointer you pass to Lock should not initially refer to allocated memory. The pointer gets set to memory already allocated by DirectX. And don't forget to call Unlock on the vertex buffer afterwards.

void Function(){  VertexStruct* pVertices;  pVertexBuffer->Lock(0, 0, &pVertices, 0);  //Do stuff to pVertices  pVertexBuffer->Unlock();}
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas nly, and not for things themselves." - John Locke o
Fruny
Fruny
Quote:
Original post by Alex Swinney
The code between the new and delete uses the two variables to read one DX vertex buffer and write to another DX vertex buffer.


Let me draw you a picture:



The assertion checks that the block header (which contains, among other things, the number of array elements allocated, so that delete[] does the right thing), is valid. You trashed it with your DirectX data, the assertion fails.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
khalligan
khalligan

Im no DirectX genius, but heres what Id do:

The pVertices void pointer in Lock() function is normally NULL

void *pVertices = NULL;pVertexBuffer->Lock(0, 0, &(void**)pVertices, 0);ter = new TerrainVertex[desc.Size/sizeof(TerrainVertex)+1];ter2 = new TerrainVertex[desc.Size/sizeof(TerrainVertex)+1];// Do stuff to vertices, careful not to go outside arrayspVertexBuffer->UnLock(...);// Then in your destructor~....(){  delete[] ter;  delete[] ter2;}


I maybe overexplained this for you, and you already understand. But what the hell
Alex Swinney
Alex Swinney
Quote:
Original post by Fruny
Quote:
Original post by Alex Swinney
The code between the new and delete uses the two variables to read one DX vertex buffer and write to another DX vertex buffer.


Let me draw you a picture:



The assertion checks that the block header (which contains, among other things, the number of array elements allocated, so that delete[] does the right thing), is valid. You trashed it with your DirectX data, the assertion fails.


Ahhh...See I looked for the error in google and in VS's search function, couldn't find a thing..

That picture really helps, thanks!
I WISH SOMEONE WOULD FIX THE DAMNED LOGIN!
Alex Swinney
Alex Swinney
Quote:
Original post by Agony
The pointer you pass to Lock should not initially refer to allocated memory. The pointer gets set to memory already allocated by DirectX. And don't forget to call Unlock on the vertex buffer afterwards.

*** Source Snippet Removed ***


Thank you, that is one of the questions I had..
I WISH SOMEONE WOULD FIX THE DAMNED LOGIN!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.