Error with "new" operator (now a different error)
I'm getting a unhandled exception when initializing a struct variable using the new operator. But the werid thing is, is it does it 3 times in a row correctly, then the 4th time it errors out.
This is the error prone code:
D3DXFRAME_DERIVED* DerivedFrame = new D3DXFRAME_DERIVED;
This is the structure:
struct D3DXFRAME_DERIVED: public D3DXFRAME
{
D3DXMATRIX CombinedTransformationMatrix;
};
This is where it errors out:
void *__cdecl operator new(size_t size) _THROW1(_STD bad_alloc)
{ // try to allocate size bytes
void *p;
while ((p = malloc(size)) == 0) <----- RIGHT HERE----------
if (_callnewh(size) == 0)
_STD _Nomemory();
return (p);
}
Ever time the new operator is used *p is NULL, and size = 144, but the variables are the same even when it errors out!
As you can tell this is the skinned mesh example from MS DirectX, but its not being very nice to me for some reason. If anyone could point me in the right direction it would really help.
Thanks.
[Edited by - TheMac on February 20, 2006 2:27:49 PM]
I got rid of the new operator and this is my entire function:
After rebooting I still get an error at the same memory address ("Unhandled exception at 0x7c901230"), except this time the error happens at line:
DerivedFrame.Name = new char[(UINT)strlen(Name) + 1];
Just right after I declared the variable.
Any ideas?
Thanks.
D3DXFRAME_DERIVED DerivedFrame;DerivedFrame.Name = new char[(UINT)strlen(Name) + 1];memcpy(DerivedFrame.Name, Name, (UINT)strlen(Name) + 1);// initialize other data members of the frameD3DXMatrixIdentity(&DerivedFrame.TransformationMatrix);D3DXMatrixIdentity(&DerivedFrame.CombinedTransformationMatrix);DerivedFrame.pMeshContainer = NULL;DerivedFrame.pFrameSibling = NULL;DerivedFrame.pFrameFirstChild = NULL;*NewFrame = &DerivedFrame;//DerivedFrame = NULL;return S_OK;
After rebooting I still get an error at the same memory address ("Unhandled exception at 0x7c901230"), except this time the error happens at line:
DerivedFrame.Name = new char[(UINT)strlen(Name) + 1];
Just right after I declared the variable.
Any ideas?
Thanks.
It has been my experience that this type of error is usually caused by a double deletion of something. Probable causes include missing copy contructors or assignment operators, see "the rule of three") for objects with pointers and misuse of static objects in a multitreaded application, among others. Such problems are notoriously difficult to track down.
I noticed that the first three times the method is called, it works fine, but on the fourth time it errors out at the above address that I listed earlier. But on the fourth time around, if I skip these two statements:
DerivedFrame.Name = new char[MAX_PATH]; //new char[(UINT)strlen(Name) + 1];
memcpy(DerivedFrame.Name, Name, (UINT)strlen(Name) + 1);
Then I can execute the rest of the method, and continue. This is very weird, I added some deconstrutor code, but it didn't help.
DerivedFrame.Name = new char[MAX_PATH]; //new char[(UINT)strlen(Name) + 1];
memcpy(DerivedFrame.Name, Name, (UINT)strlen(Name) + 1);
Then I can execute the rest of the method, and continue. This is very weird, I added some deconstrutor code, but it didn't help.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement