Problem is that for some unknown reason, my compiler insists that vec = vec2. Not that *vec = *vec2, but the actual pointer locations vec = vec2. So for example if vec = :0253FCA0 then vec2 gets the same allocation :0253FCA0. How is this possible? I mean the compiler literally KNOWS it's already allocated (he even gets it as an argument), and yet goes ahead allocating a second pointer. This isn't even random. He consistently does this after a few loops.
Funny thing is that he fails at delete vec2. Which means that for some reason he didn't even really allocate vec2...
Funny thing is that he fails at delete vec2. Which means that for some reason he didn't even really allocate vec2...
Which means there's more to the code than you've shown. You're probably assigning vec to vec2 instead of allocating a new one. In particular, I see no code that checks whether vec and vec2 point to the same object.
The more important question is, why are you allocting all those vectors with new just to destroy them two lines later? That's completely unnecessary.
There is additional code involving putting pointers in a list. Since that does not destroy pointers and kinda gives away code I don't want to, I did not add it.
The actual code of doSomething looked more like this:
1 2 3 4 5 6 7
void doSomething(Vector * vec) {
Vector * vec2 = new Vector(some numbers);
*vec2 = *vec2 + *vec;
if(list does not contain vec)
add vec2 to list;
elsedelete vec2;
}
After which I got vec2 out of the list at some other time (if it was added) and called delete vec2;
I somehow fixed things by:
1 2 3 4 5 6 7
void doSomething(Vector * vec) {
if(file does not contain vec) {
Vector * vec2 = new Vector(some numbers);
*vec2 = *vec2 + *vec;
add vec2 to list;
}
}
I think there must have been some kind of messy delete, eventhough I do not see how this code is not correct. Ofc, I'm a human being and I guess I overlooked something.
That probably didn't fix anything, chances are that it just masked the problem.
The problem being that you're attempting to do manual memory management in the first place. Why are you using new? And even if it was really necessary to use new in your situation (unlikely), why aren't you using smart pointers?
I just don't know any other way. I don't want to keep a list of Vectors because I don't know how to free its memory. So I use pointers, which I know how to free. I don't know about smart pointers though. Never heard of it.
The other way is to use the normal way, i.e. automatic objects. The entire point is that you don't have to worry about their memory, as it is handled by the implementation. It looks like this:
1 2 3 4
for (...) {
Vector vec(x,y,z);
doSomething(vec);
}
1 2 3 4 5 6 7
void doSomething(const Vector& vec) {
if (file does not contain vec) {
Vector vec2(some numbers);
vec2+=vec;
add vec2 to list;
}
}
I don't know about smart pointers though. Never heard of it.
Smart pointers (most importantly, C++11's unique_ptr and shared_ptr) exist for the occasional case where you actually have to use new. They delete the contained pointer automatically when they go out of scope.