Sign in to follow this  
mc30900

accessing a place in the memory which does not exsist

Recommended Posts

mc30900    100
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 this post


Link to post
Share on other sites
wodinoneeye    1689
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 this post


Link to post
Share on other sites
kvp    196
Quote:
Original post by mc30900
here 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 this post


Link to post
Share on other sites
mc30900    100
i have already done that...
thats not my problem.
i already used the same 'for' structure and same iterator for accessing those objects in another function.

Share this post


Link to post
Share on other sites
ApochPiQ    23003
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this