views:

69

answers:

4

I've debugged my other problem back, to the MyMesh constructor. In this code:

if (hollow) {
    numTriangles = n*8;
    triangles=new MyTriangle[numTriangles];
    if (smooth) numSurfacePoints=n*8;
    else numSurfacePoints=n*12;
    surfacePoints=new SurfacePoint[numSurfacePoints];
}else {
    numTriangles = n*4;
    triangles=new MyTriangle[numTriangles];
    if (smooth){
     numSurfacePoints=n*4;
     surfacePoints=new SurfacePoint[numSurfacePoints];
     surfacePoints[n*4]=SurfacePoint(vector3(0,0,1),vector3(0,0,1));
     surfacePoints[n*4+1]=SurfacePoint(vector3(0,0,-1),vector3(0,0,-1));
    }else{
     numSurfacePoints=n*6;
     surfacePoints=new SurfacePoint[numSurfacePoints];
     surfacePoints[n*6]=SurfacePoint(vector3(0,0,1),vector3(0,0,1));
     surfacePoints[n*6+1]=SurfacePoint(vector3(0,0,-1),vector3(0,0,-1));
    }
}

I'm determining the neccessary SurfacePoints and Triangles for the mesh. The bools "hollow" and "smooth" indicates, if i need a hole in the cone, or if the normals are the same, but i think it's irrevelant.

The problem is: if hollow==false, it does something wrong, but doesn't crashes, it even allows to put the values in the arrays, but when I'm trying to cout it like this:

for(int i=0;i<numSurfacePoints;i++){
    std::cout<<"vertex "<<i<<"-> pos:"<<surfacePoints[i].pos.x<<" "<<
     surfacePoints[i].pos.y<<" "<<surfacePoints[i].pos.z<<
     " norm:"<<surfacePoints[i].norm.x<<" "<<surfacePoints[i].norm.y<<
     " "<<surfacePoints[i].norm.z<<"\n";
}

it throws a bad_alloc exception, right when i=0.

additionally, there was a time, when the upper code segment threw a bad_alloc at the operator new, but that problem just solved itself, but maybe it's relevant.

can anybody help me?

A: 

How much is n? That's where the amount of surfacePoints is calculated by...

xtofl
for example 4. but if its not hollow, then even 20 is okay
Adam Loska
+2  A: 

You are allocating memory for N surface points then so how can you assign value to N th and N+1 th point ?

Please put check for out of array bound conditions...

Ashish
Oh, thank you very much! I was looking for it for hours, but it was this basic... bah... Anyway, thanks a lot!
Adam Loska
+1  A: 

You are writing outside the allocated memory.

With

    numSurfacePoints=n*4;
    surfacePoints=new SurfacePoint[numSurfacePoints];

the valid index range you can use is [0 ... (n*4 - 1)] (e.g for n=10 you could use index [0..39] )

but then you write

    surfacePoints[n*4]=SurfacePoint(vector3(0,0,1),vector3(0,0,1));
    surfacePoints[n*4+1]=SurfacePoint(vector3(0,0,-1),vector3(0,0,-1));

which both go beyond that (same happens with the smooth case), i.e. for n==10 you write to index 40 and 41.

Thus you are corrupting the the heap (the memory are where the 'new's are coming from). Depending on the memory layout (which can vary from one run to the next) you overwrite data which belongs to someone else (either the heap, or another part of your program). Depending on layout it will crash right away, or lay the seed to a later crash or problem.

In your case, when you do the output, the runtime library will also allocate (call malloc or new) to get some memory for what it's doing and the heap system notices that something is wrong (you overwrote some data that the heap system needs to manage the memory chunks) and throws the exception.

Nicholaz
A: 

It looks like your heap is corrupted. Check that you are not deleting allocated pointers more than once. Not accessing out-of-bounds elements of arrays. Not accessing deleted pointers, etc.

denisenkom