getting bad_alloc from new

Aug 22, 2011 at 4:15pm
I'm running a program and am getting a bad_alloc exception from new. The problem is not that I've run out of memory, my program isn't that resource-intensive.

The relevant code is the following:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
bool NFault::Generate(fstream& f, NColor& fillColor, NColor& wireframeColor)
{
	//...
	float* normal = NULL;
	try{normal = new float[3*NumVertexes]; //will hold normals of each vertex
	}catch(std::exception& e) { cout << e.what();}
	for(unsigned int i=0;i<3*NumVertexes;i++)
		normal[i]=0.f; //normals start at 0.f
	for(unsigned int i=0;i<NumIndexes;i+=3)
	{
		NVertexGeometry vx1=Geometry[Index[i]];
		NVertexGeometry vx2=Geometry[Index[i+1]];
		NVertexGeometry vx3=Geometry[Index[i+2]];
		MyNVector v1(vx1,vx2);
		MyNVector v2(vx1,vx3);

		MyNVector _normal = v1.CrossProduct(v2);

		for(int j=0;j<3;j++)
		{
			normal[3*Index[i+j]]+=_normal.x;
			normal[3*Index[i+j]+1]+=_normal.y;
			normal[3*Index[i+j]+2]+=_normal.z;
		}
	}
	for(unsigned int i=0;i<NumVertexes;i++)
		Geometry[i].SetNormal(normal[3*i],normal[3*i+1],normal[3*i+2]); 
	delete[] normal;
	return true;


This function is called twice (by two different objects). It goes through the first time just fine, but then throws an exception at line 5 the second time.

Any hints regarding what's going on?
Last edited on Aug 22, 2011 at 4:15pm
Aug 22, 2011 at 4:19pm
what does NumVertexes equal when the exception is thrown? If it's <= 0 then that would be a problem.
Aug 22, 2011 at 4:42pm
It equals 364 the first time and 382 the second. Yeah, I should've mentioned that, I feared it might've been set to 0 so I'd already checked that.
Aug 22, 2011 at 9:26pm
Strange indeed. I don't see anything here that could be causing it. The only thing I can think of would be heap corruption, but that's kind of a catch-all.

Maybe try using a vector instead. If the problem goes away then it's almost certainly heap corruption.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
bool NFault::Generate(fstream& f, NColor& fillColor, NColor& wireframeColor)
{
	//...
	std::vector<float> normal(3 * NumVertexes, 0);

	for(unsigned int i=0;i<NumIndexes;i+=3)
	{
		NVertexGeometry vx1=Geometry[Index[i]];
		NVertexGeometry vx2=Geometry[Index[i+1]];
		NVertexGeometry vx3=Geometry[Index[i+2]];
		MyNVector v1(vx1,vx2);
		MyNVector v2(vx1,vx3);

		MyNVector _normal = v1.CrossProduct(v2);

		for(int j=0;j<3;j++)
		{
			normal[3*Index[i+j]]+=_normal.x;
			normal[3*Index[i+j]+1]+=_normal.y;
			normal[3*Index[i+j]+2]+=_normal.z;
		}
	}
	for(unsigned int i=0;i<NumVertexes;i++)
		Geometry[i].SetNormal(normal[3*i],normal[3*i+1],normal[3*i+2]); 
	//delete[] normal;
	return true;



Last edited on Aug 22, 2011 at 9:26pm
Aug 22, 2011 at 11:00pm
As I said, the function is run twice, back to back. The first time it works just fine, it's just the second time that it crashes. Does that indicate anything?
Aug 23, 2011 at 5:36am
The only thing it indicates to me is heap corruption.

Are you sure you're not stepping out of bounds anywhere? I can't tell from the code you posted, and the nasty thing with heap corruption is that the problem could be anywhere in the program.

The only thing I see that's a little fishy is the 'Index' array:

NVertexGeometry vx1=Geometry[Index[i]]; // isn't 'i' the index? What do you need Index for?
Aug 30, 2011 at 3:03pm
Maybe try using a vector instead. If the problem goes away then it's almost certainly heap corruption.


Sorry for the delay, but I have just implemented it using a vector and it still gives an error. However, couldn't the problem still be heap corruption? Isn't a vector basically a convoluted array? Wouldn't it be just as vulnerable to failed allocations due to heap corruption?
Last edited on Aug 30, 2011 at 3:04pm
Sep 5, 2011 at 8:31pm
Just found my error. In a previous part of the function, I had a static variable acting as an iterator. I thought that since different instances called the function each time, it would be a new static variable that was created for each instance. I was wrong. Thus, when the second instance called the function, the iterator began at 364.

I knew static members were shared between different instances, but I thought static variables declared inside non-static functions were instantiated. My mistake.
Last edited on Sep 5, 2011 at 9:25pm
Topic archived. No new replies allowed.