| wasabi (207) | |||
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:
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
|
|||
| Disch (7378) | |
| what does NumVertexes equal when the exception is thrown? If it's <= 0 then that would be a problem. | |
|
|
|
| wasabi (207) | |
| 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. | |
|
|
|
| Disch (7378) | |||
|
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.
| |||
|
Last edited on
|
|||
| wasabi (207) | |
| 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? | |
|
|
|
| Disch (7378) | |
|
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?
| |
|
|
|
| wasabi (207) | ||
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
|
||
| wasabi (207) | |
|
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
|
|