UIView drawRect; class variables out of scope

Posted by Toby Wilson on Stack Overflow See other posts from Stack Overflow or by Toby Wilson
Published on 2010-04-26T09:59:10Z Indexed on 2010/04/26 10:03 UTC
Read the original article Hit count: 551

Short & sweet version of my last question in light of new information.

I have a UIVIew with an init and a drawrect method (and another thread and a bunch of other stuff, but I'll keep it short & sweet).

All of the class variables that I alloc and init in the -(id)init method are out of scope/nil/0x0 in the drawRect method, and I am unable to access them.

For example;

In the interface:

NSObject* fred;

In the implementation:

-(id)init
{
    if(self == [super init])
    {
        fred = [[NSObject alloc] init];
    }

    return self;
}

-(void)drawRect:(CGRect)rect
{
    NSLog(@"Fred is retained %i times",[fred retainCount]); //FAIL
    NSLog(@"But his variable is actually just pointing at uninitialised 0x0, so you're not reading this in the debugger because the application has crashed before it got here."
}

Should add that init IS being called before drawRect also. Anyone have any ideas?

© Stack Overflow or respective owner

Related posts about drawrect

Related posts about variables