Reputation: 67
So I am working on a graphics program for a college class. The teacher has a code but she the EXE. The code works. If I am in visual studio and go start the code with debugging it works fine. The program prints out instruction to the console via std::cout and then the opengl window appears and everything works as it is suppose to. However, if I go hit ctrl+f5 for run without debugging the instructions are printed out and the window appears but the content of the window is just pure white. After a minute or so the program crashes and a pop up from windows appears saying sorry the program crash or something along those lines.
If I go into the project folder and go into the debug folder and click on the .exe the instructions are printed out again and then a pop up saying “Debug assert fail” and it blames it on a vector index being out of range. This would make since except why does this not happen when I run the program in debug mode through visual studio?
If I am in visual studio and change the configuration from debug to release the instructions are printed again and this time a pop up saying bad memory alloc. It points to this line of code:
Model* mutiplyMatrix(Model model,CMatrix matrix)
{
Model* m=new Model();
for(unsigned int i=0;i<model.vectorList.size();i++)
{
CVector* v=new CVector();
v->m=matrix*(*(model.vectorList.at(i)->m));
v->drawBit=model.vectorList.at(i)->drawBit;
m->addVectorToList(v);
}
return m;
}
The exact line is
CVector* v=new CVector();
So my question is how I can get an exe I could give to my teacher. I am fine with including the needed libraries and the like. The problem is that the program works but only when I execute it from visual studio in debug mode. The program crashes when used in either release mode or without debugging.
This is visual studio 2010.
Upvotes: 2
Views: 5124
Reputation: 29240
I sounds very much like you have an uninitialized variable that you're using and that influences the number of times that loop is executed. In debug mode the value might consistently be set to a low value or 0 simply by nature of being run under the debugger. However, when not running under the debugger, you're likely ending up with an extremely large value, which in turn causes the iteration above to run for an extended period of time and then crash when it runs out of memory. I would track backwards from model.vectorList.size()
to see where the value is coming from. I would also add a cout
line before you enter the for loop that prints out what the size is.
EDIT:
Well, I still think your problem is related to unbounded memory allocation related to an uninitialized variable, but there are a number of things you doing in your code that are hindering you
Model* mutiplyMatrix(Model mode,CMatrix matrix)
This is triggering a copy of both Model and CMatrix. If your copy constructors or destructors aren't properly designed, that's going to be problematic, especially with your heavy use of raw pointer members. Consider the following class:
class Foo {
char * data;
Foo() {
data = new char[1024];
}
virtual ~Foo() {
delete[] data;
}
};
Looks fine right? I allocate the memory and I deallocate it. But what happens if I have a function
void bar(Foo foo) {
std::cout << "Called bar" << std::endl;
}
If I call that function with one of my foo objects, I'm corrupting memory. bar()
takes a Foo object, not a Foo reference, so my original Foo is simply copied before being handed to bar()
. Because I didn't define a copy constructor, the default copy constructor is invoked, which means that the pointer value is just copied. This means that when bar() ends, the pointer will be deallocated by the ~Foo() destructor on my class. But then later when my original Foo instance leaves scope, the same pointer will be deleted again. Memory corruption and crash.
At the very least your method should be declared
Model* mutiplyMatrix(Model & mode,CMatrix & matrix)
or preferably
Model* mutiplyMatrix(const Model & mode,const CMatrix & matrix)
Even worse, since we can infer from the source code below that Model has a member that looks something like this:
std:list<CVector*> vectorList;
When your incoming Model is copied and then destroyed, it's probably making big ol' mess of the heap.
My biggest piece of advice would be stop using so many damn pointers.
Upvotes: 2