Unhandled exception at 0x7c812afb in ProjectBlockSrc.exe: Microsoft C++ exception:
std::bad_alloc at memory location 0x0012fbc4..[/quote]
The program ony has 21,760k allocated when it throws this error.
I just don't understand.
The following line throws the error. I just don't understand it was working perfect.
world0 = new world(1);
It's not a problem with the world class, because I can attempt to allocate another class there and it fails also.
What could cause this?
Thanks,
walker
Unhandled exception at 0x7c812afb
I am making a program in C++, while trying to create a new instance of a class I get an error.
The following line throws the error. I just don't understand it was working perfect.
world0 = new world(1);
I don't know what a 'world' is, and you say it "was" working perfect, so maybe you can find out what changed? At a guess, it's trying to allocate a squillion bytes of ram and failing, either due to the definition of world or because your program has scribbled some ram somewhere.
"Was" working also suggests that you may be corrupting the heap somewhere. What are your allocation patterns - could fragmentation be a problem? Either way, we need more code - obviously bad_alloc is thrown when an allocation fails, so what you've shown tells us very little.
Here is the code that is related this problem.
Thanks in advance, because this is just killing me.
I want to know the problem, so I can prevent it in the future as well.
Here is all the code from the entry point til the line it crashes:
Main.cpp snipplet
Here is the world class constructor:
Map.cpp:
Thanks in advance, because this is just killing me.
I want to know the problem, so I can prevent it in the future as well.
Here is all the code from the entry point til the line it crashes:
Main.cpp snipplet
int main( int argc, char *argv[] )
{
if( fv::init() == false ) { return 1; }
iVertex2d screen = fv::getScreenRes();
fv::createWindow(screen.x-128,screen.y-128,32,"Project Block");
fv::initSound();
initGL();
fv::text::init("resources/fonts/main.ttf",255,255,255,16);
running = true;
SDL_ShowCursor(SDL_ENABLE);
srand ( time(NULL) );
//init FPS data
for (unsigned char a=0; a<60; a++)
{
fpsData[a] = 0;
}
if (loadResources() == false)
{
return 1;
}
//init thread variables
for(int a=0;a<thread1QueneLimit;a++)
{
thread1Quene[a].state=NOTEXIST;
}
for(int a=0;a<thread1QueneLimit;a++)
{
thread1Done[a].state=NOTEXIST;
}
//Generate Block Data
for (int y=0;y<4;y+=1)
{
for (int x=0;x<16;x++)
{
block[x+(y*16)].xMin = tsMax(x)+0.0001f;
block[x+(y*16)].xMax = tsMin(x)-0.0001f;
block[x+(y*16)].yMin = tsMin(y*3)+texOffset-0.0001f;
block[x+(y*16)].yMax = tsMax(y*3)+texOffset+0.0001f;
block[x+(y*16)].draw = drawFullBlock;
block[x+(y*16)].reacts = false;
block[x+(y*16)].flammable = false;
block[x+(y*16)].hard = true;
block[x+(y*16)].group = static_cast<objBlock>(x+(y*16));
block[x+(y*16)].amount = 0;
block[x+(y*16)].data = 255;
}
}
fps = new Timer();
Writer = new file();
Here is the world class constructor:
Map.cpp:
world::world(int pName)
{
drawSkyBox=true;
name = pName;
//time=90.00f;
//timeAdd=-0.01f;
time = 45.00f;
timeAdd=0.01f;
//Make Chunks
//xChunks=CLIPDIST;
//yChunks=CLIPDIST;
//xOffset=((CLIPDIST-1)/2)+1;
//yOffset=((CLIPDIST-1)/2)+1;
//Set all the chunks to nonexisting
for (int a=0;a<CLIPDIST;a++)
{
for (int b=0;b<CLIPDIST;b++)
{
for (int c=0;c<CLIPDIST;c++)
{
chunkExists[a][c]=false;
}
}
}
init=true;
wait=WAITTIME;
running=false;
xArray = HALFCLIPDIST;
yArray = HALFCLIPDIST;
zArray = HALFCLIPDIST;
//mLoading = boost::mutex;
//Task manager
/*taskMax=0;
for (int a=0; a<(CLIPDIST*CLIPDIST*CLIPDIST); a++)
{
task[a].State = done;
}*/
//Init Light
lightLeft = 0.85f;
lightRight = 0.85f;
lightUp = 1.00f;
lightDown = 0.70f;
lightBottom = 0.50f;
lightTop = 1.00f;
}
If you are using an IDE that supports breakpoints, put one on every line and step through the constructor until the exception gets thrown.
At first glance, my gut says
is the likely suspect, but since we don't know how [font="Arial"]chunkExists [/font][font="Arial"]was declared, it is hard to say. My guess it is smaller than you think it is (smaller than [CLIPDIST][CLIPDIST][CLIPDIST]) and your writing into memory you shouldn't be.[/font]
[font="Arial"]
[edit]
Fixed formatting in code-block[/font]
At first glance, my gut says
for (int a=0;a<CLIPDIST;a++)
{
for (int b=0;b<CLIPDIST;b++)
{
for (int c=0;c<CLIPDIST;c++)
{
chunkExists[a][c]=false;
}
}
}
is the likely suspect, but since we don't know how [font="Arial"]chunkExists [/font][font="Arial"]was declared, it is hard to say. My guess it is smaller than you think it is (smaller than [CLIPDIST][CLIPDIST][CLIPDIST]) and your writing into memory you shouldn't be.[/font]
[font="Arial"]
[edit]
Fixed formatting in code-block[/font]
post your header files as well. My guess is you are running over an array bounds with all those array initialization.
It's not the array in the world initializer because when I put a break-point on
the line that reads world0= new world() it throws the error before
entering the initializer.
Also I just replaced the line world0= new world(), with the line menu0 = new menu(); and it still throws the same error.
So It's not specific to the world class.
However here is the world header.
Also, CLIPDIST = 25
World.h:
the line that reads world0= new world() it throws the error before
entering the initializer.
Also I just replaced the line world0= new world(), with the line menu0 = new menu(); and it still throws the same error.
So It's not specific to the world class.
However here is the world header.
Also, CLIPDIST = 25
World.h:
class world
{
public:
world(int pName);
void draw();
void drawChunkMap();
void step();
bool checkCollision(vertex Point);
//void checkOffset();
//Threads
void startThread();
void thread1loop();
void thread1add(int x, int y, int z);
void thread1recieve();
void thread1send();
void thread1shift(signed int x, signed int y, signed int z);
void thread1addDone(int x, int y, int z, chunk* chk);
//Offsets
void xOffsetAdd();
void xOffsetSub();
void yOffsetSub();
void yOffsetAdd();
void zOffsetSub();
void zOffsetAdd();
//===============================
boost::thread tLoader;
iVertex3d tmpPoint;
iVertex3d tmpOffset;
vertexsi tmpLiquidPoint;
vertexsi tmpLiquidOffset;
vertex tmpInitPoint;
vertexsi tmpInitOffset;
//Old
//void setTarget(float x, float y, float z, int xChunkOffset=0, int yChunkOffset=0, int zChunkOffset=0);
//New
void setTarget(int x, int y, int z);
void setTarget(int x, int y, int z, signed int xChunkOffset, signed int yChunkOffset, signed int zChunkOffset);
void setTarget(iVertex3d block,iVertex3d chunk);
void setTarget(int blockX, int blockY, int blockZ,iVertex3d chunk);
void setLiquidTarget(float x, float y, float z, signed int xChunkOffset, signed int yChunkOffset, signed int zChunkOffset);
void setBlock(unsigned char block);
void setBlock(unsigned char block, unsigned char data);
void setData(unsigned char data);
void playerSetBlock(unsigned char thisBlock);
void playerSetBlock(unsigned char thisBlock, unsigned char data);
void setBlockAmount(int amount);
void blockLiquidBufferAdd();
void delBlock();
void playerDelBlock(iVertex3d chunk, iVertex3d pos);
void render();
void renderMakeList();
vector<iVertex3d> renderList;
bool activeExists();
bool blockHardExists();
bool blockExists();
bool blockExistsPlus();
bool blockFlammable();
bool blockHard(int x, int y, int z, int xChunkOffset, int yChunkOffset, int zChunkOffset);
int getBlockAmount();
//bool blockSpecial();
//For use by Chunk->Render()
unsigned char getBlock(int x, int y, int z, int xChunkOffset, int yChunkOffset, int zChunkOffset);
unsigned char getData(int x, int y, int z, int xChunkOffset, int yChunkOffset, int zChunkOffset);
int getBlockAmount(int x, int y, int z, int xChunkOffset, int yChunkOffset, int zChunkOffset);
unsigned char getBlock();
unsigned char getData();
//unsigned char* getBlockPtr();
//===============================
void loaderCreate();
void loaderMain();
void checkChunks();
void bufferInitAll();
bool chunkVisible(int x, int y, int z);
//liquid* createLiquidBody(float amount);
//void liquidBufferAdd(signed int x, signed int y, signed int z);
//void blockAdd(signed int x, signed int y, signed int z, int tx, int ty,float amount,signed int xChunkOffset=0, signed int yChunkOffset=0, signed int zChunkOffset=0, bool ignore = false);
//void blockSetTexture(signed int x, signed int y, signed int z,int tx,int ty,signed int xChunkOffset=0, signed int yChunkOffset=0, signed int zChunkOffset=0);
//bool blockGetFlameable(signed int x, signed int y, signed int z, signed int xChunkOffset, signed int yChunkOffset, signed int zChunkOffset);
//vertexi2d blockGetTexture(float x, float y, float z, signed int xChunkOffset=0, signed int yChunkOffset=0, signed int zChunkOffset=0);
//float blockGetAmount(signed int x, signed int y, signed int z,signed int xChunkOffset=0, signed int yChunkOffset=0, signed int zChunkOffset=0);
//void blockSetAmount(signed int x, signed int y, signed int z,float amount, signed int xChunkOffset=0, signed int yChunkOffset=0, signed int zChunkOffset=0);
//bool hardBlockExists();
void chunkDump();
void saveChunks();
void drawSkydome();
// Was +2 ?
chunk* Chunk[CLIPDIST][CLIPDIST][CLIPDIST];
bool chunkExists[CLIPDIST][CLIPDIST][CLIPDIST];
void bufferInit();
int xOffset,yOffset,zOffset;
int xArray,yArray,zArray;
//fVertex tmpEntry;
int wait;
int name;
bool drawSkyBox;
float time;
float timeAdd;
//Tasks
void taskAddWrite(chunk* newChunk);
void taskAddRender(signed int x,signed int y,signed int z);
void stepAddRender(iVertex3d pos);
void stepRender();
void taskAddRead(signed int x,signed int y,signed int z);
bool taskPerform();
int taskMax;
//chunkTask task[(CLIPDIST*CLIPDIST*CLIPDIST)];
private:
int xChunks, yChunks;
bool init;
deque<chunkTask> queue;
deque<chunkTask> loaderQueue;
deque<iVertex3d> renderQueue;
boost::mutex mLoading;
boost::mutex mRender;
};
Just because an exception occurs at a specific line of code does not mean the actual error is on that line of code. If you overrun an array in the beginning of your program, it will cause errors exactly like what you are getting.
The code so far looks fine. Where is the header file for . . .
fpsData
thread1Quene
and
block
The code so far looks fine. Where is the header file for . . .
fpsData
thread1Quene
and
block
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement