Jump to content
  • Advertisement
Sign in to follow this  
TheMac

Error with "new" operator (now a different error)

This topic is 4808 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

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]

Share this post


Link to post
Share on other sites
Advertisement
I got rid of the new operator and this is my entire function:

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 frame
D3DXMatrixIdentity(&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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!