accessing a place in the memory which does not exsist

Recommended Posts

hello all. for some reason my game crashes (its in C++) i have found the place where it crashes but i have no real idea what is causing this. here is the function that makes it crash:
int collision(int to, float placex, float placey,float wid,float  hei)
{

if (to==0)
{

for (objiterator = objects.begin(); objiterator !=objects.end(); objiterator++)
{
if ((*objiterator)->collisionn == 1)
{
if (((*objiterator)->placey > (placey+0.1)) && ((placey+0.1)> ((*objiterator)->placey-((*objiterator)->height))))
{
if ((((*objiterator)->placex) < (placex)) && ((placex) < ((*objiterator)->placex+(*objiterator)->width)))
return 0;
if ((((*objiterator)->placex) < (placex+wid) )&& ((placex+wid) < ((*objiterator)->placex+((*objiterator)->width))))
return 0;
}
}
}
}
return -1;
}


when i tried to run the debugger i got the next warning: An Access Violation (Segmentation Fault) raised in your program if anyone has any idea what can cause this please tell me. thanks in advance

Share on other sites
That usually is a pointer problem -- when you try to access memory with a out of range pointr value. (its pointing out into lala-land into a segment your program doesnt have permission to access)

I dont see the definition for objiterator. You are treating it like a 'pointer to a pointer to an object' (*objiterator)->placey are you sure its not supposed to be a 'pointer to an object' which would be used like objiterator->placey in all those places ???

You can put a breakpoint in the loop (right after the for statement and look at the value of objiterator (and probably see what it is pointing to). Possibly if the iteration operataor code is wrong it might be supplying a bad value at some point (depending on where the code is failing)

Share on other sites
here is the definition:
vector <new_object *> objects;vector <new_object *>::iterator objiterator;

Share on other sites
Quote:
 Original post by mc30900here is the definition:*** Source Snippet Removed ***

I hope you have filled up the pointer vector with real objects. Because that vector only contains pointers to objects, not the objects themselves. You must create new objects and add() them to the list, not just resize the list.

Share on other sites
thats not my problem.
i already used the same 'for' structure and same iterator for accessing those objects in another function.

Share on other sites
What line does the crash occur on? What are the full details of the crash message (address, read-vs.-write, and so on)? Also, post any code that deletes objects from the vector.

Chances are you've freed an item and not taken it out of the vector, which is causing the problems. It's also possible that you've modified the vector and thus invalidated your iterators, but unless you're running multiple threads I don't see an obvious way for that to happen.

Create an account

Register a new account

• Partner Spotlight

• Forum Statistics

• Total Topics
627667
• Total Posts
2978539

• 10
• 10
• 10
• 12
• 22