Skip to main content
gamedev.net
🔒 Locked

help needed :( Memory leak

Started by Striken Mar 10, 2004 at 3:11 PM 3 replies 350+ views
Original Post
Striken
Striken
I''ve got a problem with pointers and memory etc. Ok here''s the situation. I create a mesh temporarily. Mesh details get stored i.e.: tempMesh.face[1].vert = new CVertexID[3]; the function ends. the temporary mesh is drawn. >> the old mesh must be DELETED << a new temporary mesh is created the function ends etc. What I need to do, is (i presume) store the location of the array of (in this example''s case) CVertexID, and delete[] the contents of that on the next pass around. So I''m thinking something along the lines of CVertexID *p_oldVert; p_oldVert = &tempMesh.face[1].vert; then on next time the function is called: delete[] p_oldVert; OR SOMETHING ALONG THOSE LINES! ** confused ** Since the function MUST end before it is drawn, I think the correct references go out of context and so I can''t access them correctly. Please if you have any insight, I have been stuck with this problem for about a week or two now. This is a core function so the memory leak over time is unacceptable!
Teach a programmer an answer, he can code for a day. Show a programmer the documentation, he can code for a lifetime.
smart_idiot
smart_idiot
// vert of the 2nd element of tempMesh.face points to 3 CVertexID objects. So far so good.
tempMesh.face[1].vert = new CVertexID[3];

// Pointer to a vertex. Also okay.
CVertexID *p_oldVert;

// Assigning a pointer to the pointer to the CVertexID array. They are different types, this should generate a warning or something.
p_oldVert = &tempMesh.face[1].vert;

// No good can come from this; it''s pointing to a) the wrong type, and b) it points to the inside of another object.
delete[] p_oldVert;
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other
Striken
Striken
Ok thanks, but it still doesn''t help me get anywhere near an answer!

I think I conceptually understand the problem, it''s just implementing it I''m not entirely sure about. Here''s the deal:

1) Create an array of pointers
2) Store the Address of the array of Pointers
3) Draw the Mesh structure
4) Delete the array of pointers using the address
5) Re-Create array of pointers.

So I create the array of pointers:
tempMesh.face[0].vert = new CVertexID[3];

Store the address:
?? How do I define the variable that stores the address?
CVertexID ?? I''m not sure about this.

Draw the Mesh...

Delete the array
delete[] p_arrayOldVertexID

reassign

Please help
Teach a programmer an answer, he can code for a day. Show a programmer the documentation, he can code for a lifetime.
ForeverStarlight
ForeverStarlight
Striken,

What smart_idiot was trying to say, is that:

tempMesh.face[1].vert

is a pointer to the CVertexID array, that means:

&tempMesh.face[1].vert;

is a pointer to a pointer. . . So,

p_oldVert = tempMesh.face[1].vert;

would be the correct way of pointing p_oldVert to the array. There is also the fact that tempMesh.face[1].vert is still has the address of the array even after you delete it through p_oldVert. So make sure that is not a problem. . . tempMesh is a local variable of the function then you are safe, as the object is destroyed before the array.

Or you could redesign to avoid this seperation by passing tempMesh as a reference parameter , and delete the array directly after the function adn drawing is done.

Hope I understood your problem correctly and this helps.
Striken
Striken
It''s hurting my brain this problem! To make matters worse is that the whole thing is being referenced as a pointer for the draw function as an external dll so I can''t just take it out and redesign it without completely overhauling the system.

your help has brought me closer to understanding exactly what''s going on, thanks a million guys

I''ll let you know how it goes.
Teach a programmer an answer, he can code for a day. Show a programmer the documentation, he can code for a lifetime.

Topic Locked

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

Sign in to reply to this topic.