• Advertisement

Archived

This topic is now archived and is closed to further replies.

Slight problem with memory allocation...

This topic is 5061 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

In my level editor, each object has it's own class, derived from a base CFusionEditorObject class. This has worked very well up to now. However, I'm now having some trouble with memory allocation. My brush object allocates x vertices, based on however many vertices are required to build the requested brush. I allocate them when the brush is created, using the new keyword, like so:
m_pVertices = new vec3_t[iNumSides];  
The pointer to the vertices is declared as vec3_t *m_pVertices; and I didn't have any problems with this in other versions of the program which didn't use classes. As a brush can be dragged out in the editor's windows, it's necessary to destroy the old brush and create a new one for each mouse move message the window recieves (perhaps not the most efficent method, but it works for the time being). I destroy the vertex list, using the delete keyword, like so:
delete [] m_pVertices;
m_pVertices = NULL;
m_iNumVertices = 0;  
However, it's slightly after this, hidden in the MFC framework, that the program crashes, with either an "Out of memory" or a "User called breakpoint" error. I think it's something to do with the memory allocation pool, but it could be me misusing the new and delete keywords. However, I was using calloc and free, and got the same errors, so I switched to new and delete to try and get rid of the problem. It didn't work. Can anyone see what I'm doing wrong? Thanks in advance...
Windows 95 - 32 bit extensions and a graphical shell for a 16 bit patch
to an 8 bit operating system originally coded for a 4 bit microprocessor,
written by a 2 bit company that can't stand 1 bit of competition.
[edited by - iNsAn1tY on April 10, 2004 9:14:49 AM]

Share this post


Link to post
Share on other sites
Advertisement
m_pVertices = new vec3_t[iNumSides]; // iNumSides

delete [] m_pVertices; // I recommend if()..
m_pVertices = NULL;
m_iNumVertices = 0; // iNumVertices


I don''t have all your code, but do iNumSides and m_iNumVertices have something to do with eachother?

--
You''re Welcome,
Rick Wong

- sitting in his chair doing the most time-consuming thing..

Share this post


Link to post
Share on other sites
iNumSides is passed into the Create function to specify the number of sides the brush should have. m_iNumVertices is an attribute of the brush object, which just keeps track of the number of vertices it has (m_iNumVertices is generally iNumSides * 2). I already have an if statement around that little deletion code block, which checks if m_pVertices is NULL. It is initialized to NULL in the brush's constructor. However, I don't even think that passing a NULL pointer to delete would cause the problem I'm having.

Here's some more info on the error from the debugging I've run. The actual error (which is the same whether I use new/delete or calloc/free) is "User breakpoint called from code at 0x77f75a58". This occurs directly after the call to either free or delete. When I stop debugging, the following message is displayed: "HEAP[Fusion Editor.exe]: Heap block at 003264D8 modified at 00326540 past requested size of 60".

EDIT: I mean, surely you must be able to dynamically allocate memory inside classes?


Windows 95 - 32 bit extensions and a graphical shell for a 16 bit patch
to an 8 bit operating system originally coded for a 4 bit microprocessor,
written by a 2 bit company that can't stand 1 bit of competition.


[edited by - iNsAn1tY on April 10, 2004 9:45:47 AM]

Share this post


Link to post
Share on other sites
In the brush class constructor, I initialize everything to NULL. Then the Create method is called, which allocates memory. Then the Reinitialize method is called which frees the memory. The error occurs in the Reinitialize method, when I attempt to free the memory...


Windows 95 - 32 bit extensions and a graphical shell for a 16 bit patch
to an 8 bit operating system originally coded for a 4 bit microprocessor,
written by a 2 bit company that can''t stand 1 bit of competition.

Share this post


Link to post
Share on other sites
If you comment the delete[] part out, and compile in debug mode, and there''s no problem, then there''s something that''s making m_pVertices not-NULL before you call the deletion codeblock.

--
You''re Welcome,
Rick Wong

- sitting in his chair doing the most time-consuming thing..

Share this post


Link to post
Share on other sites
Hahahaha. Goddamn. It was my fault all along. The code should read:
m_iNumVertices = iNumSides * 2;
m_pVertices = (vec3_t *)calloc(m_iNumVertices, sizeof(vec3_t));
or
m_iNumVertices = iNumSides * 2;
m_pVertices = new vec3_t[m_iNumVertices];
I was only allocating half the number of vertices I needed, which meant that in the code which followed, I was trying to initialize vertices which didn't exist. Duh! Goddamn it I feel stupid . Today's lesson: check your code before posting to GameDev!

[edited by - iNsAn1tY on April 10, 2004 9:58:49 AM]

Share this post


Link to post
Share on other sites
LOL.. You said it in your first post, and I quoted you also. Hmm.. need glasses

Anyway, good luck with this project!!

--
You''re Welcome,
Rick Wong

- sitting in his chair doing the most time-consuming thing..

Share this post


Link to post
Share on other sites

  • Advertisement