Archived

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

the_grip

Memory Question

Recommended Posts

Just when i think i've got it all down... doh! Here's the deal - i have a DWORD buffer in a class that i assign values to. However, i get all kinds of problems in the destructor when it goes to delete the buffer. Example:
//this is in the class
DWORD *m_pwObjectIndices; //member variable for the class in the header
m_pwObjectIndices = new DWORD[36]; //i know the size necessary

for(DWORD nIndex = 0; nIndex < 36; nIndex+=3)
{
     m_pwObjectIndices[nIndex]		= nIndex;
     m_pwObjectIndices[nIndex+1]	= nIndex+1;
     m_pwObjectIndices[nIndex+2]	= nIndex+2;
     m_pwObjectIndices[nIndex+3]	= nIndex;
     m_pwObjectIndices[nIndex+4]	= nIndex+2;
     m_pwObjectIndices[nIndex+5]	= nIndex+3;
}
  
i understand that what is probably going on is that local variables are assigned to the DWORD buffer, then they are no longer valid after leaving the function. However, i've tried memcpy and some other things, and i still get problems. Weird thing is i don't get memory leaks when i don't delete the buffer. Any help out there? Thanks!!! "You call him Dr. Grip, doll!" [edited by - the_grip on November 5, 2002 3:42:21 AM]

Share this post


Link to post
Share on other sites
Are you sure they''re leaks and not "damage after normal block"? You''re overrunning the array and writing after your 36 elements.

The for loop will step up to 33, and you''ll write to the 33+5 = 38th element, which is outside the array.


Helpful links:
How To Ask Questions The Smart Way | Google can help with your question | Search MSDN for help with standard C or Windows functions

Share this post


Link to post
Share on other sites