Skip to main content
GameDev.net gamedev.net
🔒 Locked

Please Help With Silly Vector Problem...

Started by Sephirox Jul 9, 2004 at 12:33 PM 6 replies 400+ views
Original Post
Sephirox
Sephirox
hi this is y first post on GameDev, but u guys have helped me out alot b4;) neways, im havin a problem with the vector container class... my "game" was working fine, and had no problem with this vector, then, after i had fixed up my physics a bit (made no change to the vector code or even the Force code), all of a sudden it crashes... heres the code...i cant see a problem, i use vectors all the time. the program is in DirectDraw(lol), it crashes once i call the function containing this code... for (vector::iterator It = Forces.begin(); It != Forces.end(); It++) { if((*It)->LifeSpan <= (*It)->Dur) { UpForce += (*It)->UpForce; RightForce += (*It)->RightForce; } (*It)->LifeSpan += TimeDiff; } The vector is empty when i am first calling it, but i tried adding a Force in, which changed nothing... i know for sure that the program crashes when it runs the code between the () of the 'for', cause i put a MessageBox within the {} and it doesnt show up... can you help?
Sephirox
Sephirox
good idea...thx...
it tells me unhandled exception, access violation...

another question..do you give vectors pointers to the data, or the actual data (will they copy data or just point to it)?
tomek_zielinski
tomek_zielinski
You can do it either way...

Passing pointers can be faster bcause each pointer occupies only 4 bytes of memory, so whe your vector is being reallocated it only copies size()*4 byte and if you pass actual data the it have to copy all the stuff by using copy contructors...

But accesing pointers can be a bit slower due to dereferencig it,
and pointers have to be delete[]d before you clear() a vector in order o avoid memory leak!
And when you pass data yopu need you can just clear() your vector and destructors will do the rest.

You have to choose the best way for you on your own...
www.tmreality.com
Sephirox
Sephirox
thx

its still happenins tho...
i found out the problem happens when i set the iterator to the begining of the vector:

vector::iterator It = Forces.begin();


how can that be wrong?
Sephirox
Sephirox
ok...this is really bothering me now lol

i created another vector and didnt add anything to it...
when i set an iterator to its beginning, it worked fine, nothing crashed. i have this elsewhere in my code as well. so it cant be becuase the vector is empty. the only thing i can think of is that having the vector in my class is messing it up, cause the debugger tells me there is an access violation with "this"...very strange...
any more suggestions?
AndyTX
AndyTX
It's possible that the object instance that you are calling this function (the whole thing that you posted) on is not valid (ie. maybe an uninitialized or already deleted pointer).

It will thus run fine with all stack accessed, but the first time it tries to access a member variable you will receive an access violation. This trying to access "Forces" could cause this.

Your error about an access violation with "this" seems to support my theory. Just food for thought.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.