tags:

views:

86

answers:

2

I have a class which must return a CGRect from one of its methods:

-(CGRect)myRect
{
    CGRect rect = CGRectMake(self.mySprite.position.x,self.mySprite.position.y,self.mySprite.textureRect.size.width, self.mySprite.textureRect.size.height);
    return rect;
}

I get an exc_bad_access as soon as i try to access the mySprite ivar.

Thing is if i call it, the instance variable mySprite is full of garbage. BUT if i change the function to return void, self.mySprite does contain the correct data.

-(void)myRect
{
    CGRect rect = CGRectMake(self.mySprite.position.x,self.mySprite.position.y,self.mySprite.textureRect.size.width, self.mySprite.textureRect.size.height);
    return rect;
}

that does not crash when accessing mySprite...

+1  A: 

It might be that the compiler recognizes that the code in your second example does nothing and optimizes it away.

Ole Begemann
maybe, but most optimizers won't remove a external function call because it might have side effects.
progrmr
actually, you might be right, i just looked at the definition of CGRectMake and it's defined as an inline function, so it may be getting totally optimized away. But the OP's real problem is mySprite is bad.
progrmr
I am using mySprite in lots of function calls that return void (or other types) fine. If mySprite was really bad, shouldn't it crash in those anyways?
pabloruiz55
A: 

I could not solve this, i tried everything. All the objects accessed are ok,if i try to get a property from them just outside that method i get correct data, if i change the method to return anything else it works fine.

I solved this by having a general method on the main class and every object calls it passing itself to it. This way it works fine.

pabloruiz55