Hey all!
I'm writing a simple fluid game in C++, and I've come across a strange issue. I allocate a whole bunch of arrays of float for use in simulations. During the course of the program, nothing goes wrong. But when I try to delete the arrays, I get a SIGTRAP error (on windows using MinGW). This is a problem as I need to delete the arrays whenever I resize the game.
Here's the code where I initialize the arrays:
if(!inited){
//easy
sizexy=sxy;
size=sizexy.x*sizexy.y;
#define FLOATSPACE (float*)malloc(size*sizeof(float))
div = FLOATSPACE;
velx = FLOATSPACE;
vely = FLOATSPACE;
velx0 = FLOATSPACE;
vely0 = FLOATSPACE;
accx = FLOATSPACE;
accy = FLOATSPACE;
dens = FLOATSPACE;
dens0 = FLOATSPACE;
#undef FLOATSPACE
inited=true;
}
In that code, sxy
is just a struct containing two ints, x and y. They are never negative, so that's not the issue. That part of the code runs fine and never gives me any problems.
But here's the code where I delete the arrays:
Game::~Game(){
free(velx); //this line gives SIGTRAP
free(vely); //changing the order doesn't matter
free(velx0);
free(vely0);
free(accx);
free(accy);
free(dens);
free(dens0);
free(div);
}
I know for a fact that all of those arrays (they are float*
) are either NULL
or have been initialized with (float*)malloc(size*sizeof(float))
. I also know that my application is memory safe otherwise. Other parts of the game don't use dynamic memory. Other parts of my application do, but that's code that wrote years ago and use everywhere, it's guaranteed to be correct.
Any help?
Thanks.