I am making an object that converts any four points into tessellated plane with a certain precision. To do that, I have an array of "Point3D"s that I use to store information for where each part is do be drawn. Most of this is no problem, just some simple math. I probably have an error with the implementation somewhere to make the boxes to draw properly (I couldn't test it because I am caught in this snag). I have encountered a similar error in another project, but I forgot how I overcame it.
I would rather not post all of Point.h and Point3D.cpp since they are fairly large and does not contain any pointers (its a fairly simple class with 3 private doubles with implementation though it's various methods). However, the error is isolated within this small piece of code as you can see below. The program causes a sigabrt at free() which I noted in the code. The full error reported to me from XCode was:
"Project(11947,0x7fff765d5960) malloc: *** error for object 0x10035df88: incorrect checksum for freed object - object was probably modified after being freed.
*** set a breakpoint in malloc_error_break to debug"
Which doesn't make too much sense to me since free is ment to dealloc the entire variable at that location, not read it. I also set a breakpoint to see the memory before I tired freeing it and something seems to be there.
Code:
void Plane3D::drawTessellated(int tessCountX, int tessCountY)
{
//assuming:
//a---b
//| |
//d---c
Point3D *pts,left,right;
int pos;
pts = (Point3D*)malloc(tessCountX*tessCountY*sizeof(Point3D));
if (!pts){errLog.print("Could not malloc. [Plane3D::drawRessellated(int,int)]");return;}
pos = 0;
for (double y=0;y<=1.0;y+=1.0/tessCountY)
{
avg(a,d,y,left);
avg(b,c,y,right);
for (double x=0;x<=1.0;x+=1.0/tessCountX)
{
avg(left,right,x,pts[pos]);
pos++;
}
}
glBegin(GL_QUADS);
for (int y=0;y<tessCountY-1;y++)
{
for (int x=0;x<tessCountX-1;x++)
{
pts[x +( y *tessCountX)].useAsVertex();
pts[x+1+( y *tessCountX)].useAsVertex();
pts[x+1+((y+1)*tessCountX)].useAsVertex();
pts[x +((y+1)*tessCountX)].useAsVertex();
}
}
glEnd();
if (pts){free(pts);} // death by sigabrt
}
void Plane3D::avg(Point3D in0, Point3D in1, double input, Point3D &output)
{
output.set(avg(in0.getX(), in1.getX(), input),
avg(in0.getY(), in1.getY(), input),
avg(in0.getZ(), in1.getZ(), input));
}
double Plane3D::avg(double in0, double in1, double input)
{
return (in0*(1.0-input))+(in1*input);
}
Point3D& Point3D::set(double x,double y,double z)
{
this->x=x;
this->y=y;
this->z=z;
return *this;
}
I sure hope it is a simple fix. Thanks for your help!
EDIT: Changed the max ranges inside the for statements to stay within malloced bounds of the array