"new" fails. [c++]

Started by
4 comments, last by SiCrane 14 years, 1 month ago
Hello! I am totally confused. The Map-Loading code of my engine seems to fail at a really stupid location. The loader loads the Object-Class-IDs, like WTexture or WMesh, and creates the right class via "new". This worked fine, before I built in a new class for textures. Its not the problem that this class fails every time, no. It works fine, too, but this makes the problem so stupid: Here is what the loader does:

Load Class Num -> Mesh
Create Object from Class Num -> Obj=new WMesh;
Load Content -> OK.

--- Next object --- 

Load Class Num -> Shader
Create Object from Class Num -> Obj=new WShader;
Load Content -> OK.

--- Next object --- 

Load Class Num -> Texture (!!!)
Create Object from Class Num -> Obj=new WTexture; (WORKS FINE!!!!!!)
Load Content -> OK. 


--- Next object --- 

Load Class Num -> Mesh
Create Object from Class Num -> Obj=new WMesh;
Load Content -> OK. 

--- Next object --- 

Load Class Num -> Texture (!!! The second one !!!)
Create Object from Class Num -> Obj=new WTexture; (<--- Fails with std::bad_alloc Why???)
Load Content -> ---. 

Here is the piece of code which creates the new object:

case WTEXTURE:
   *Out=new WTexture;
return true;

Nothing special here! But why does it fail!? It's not that I don't have enougth RAM, if you think that... help me! :(
Advertisement
Can we see the constructor for WTexture? It is more likely that something in the construction process is causing the bad_alloc. Likely suspects are things like dodgy dynamic array sizes or sizes for std::vectors etc.
I checked this allready. But here you are:

WTexture::WTexture(){	MyClass=WTEXTURE;}WTexture::~WTexture(){}

And remember: It works for the first time. When you need more code, just ask :)

EDIT:

Here is the full error output from VC: (Translated from german)

One exception (first Chance) at 0x77388915 in WTech DX10.exe: 0xC0000005: access violation (I don't know the word :) ) at 0x01e6ff78.One exception (first Chance) at 0x774e42eb in WTech DX10.exe: Microsoft C++-exception : std::bad_alloc at memorylocation 0x0012e7f4..unhandled exception at 0x774e42eb in WTech DX10.exe: Microsoft C++-exception : std::bad_alloc at memorylocation 0x0012e7f4..


This all is written by "new". Hope that helps...
Quote:Original post by WuTz
(I don't know the word :) )

Probably "reading" or "writing" (doesn't really matter which one it is to us -- just figured I'd help with vocabulary :))
You probably have a buffer overflow in another buffer previously allocated that is causing corruption in the linked lists malloc() is using.

Overload all of the global new/delete operators and for each allocation, use malloc() to allocate 1,024 bytes extra beyond what is actually requested. If the error goes away, it is because you have given your buffer overflow enough padding that it no longer destroys the internal linked list required for allocations. You can work on solving the problem from there.


L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

It'd probably be easier to just run the program with Application Verifier with memory checks.

This topic is closed to new replies.

Advertisement