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.
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.
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.
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?
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?
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?
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.