views:

219

answers:

3

I'm developing a graphing application and am attempting to change the renderer from OpenGL to Quartz2D to make text rendering easier.

A retained NSDate object that was working fine before suddenly seems to be deallocating itself, causing a crash when an NSMutableString attempts to append it's decription (now 'nil').

Build & analyse doesn't report any potential problems.

Simplified, the code looks like this:

NSDate* aDate

-(id)init
{
    aDate = [[NSDate date] retain]
    return self;
}

-(void)drawRect(CGRect)rect
{
    NSMutableString* stringy = [[NSMutableString alloc] init];

    //aDate is now deallocated and pointing at 0x0?
    [stringy appendString:[aDate description]]; //Crash
}

I should stress that the actual code is a lot more complicated than that, with a seperate thread also accessing the date object, however suitable locks are in place and when stepping through the code [aDate release] is not being called anywhere.

Using [[NSDate alloc] init] bears the same effect. I should also add that init IS the first function to be called.

Can anyone suggest something I may have overlooked, or why the NSDate object is (or appears to be) releasing itself?

A: 

not tested. but won't it be a lot simpler by changing:

[stringy appendString:[aDate description]];

to

[stringy appendString:[[NSDate date] description]];

and skip all those NSDate retain stuff?

ohho
Thanks for the suggestion, but one of the NSDate objects is a persistant application launch time that is used for reference; the other two are set to the left so I can't use that method.
Tobster
+1  A: 

Try to use aDate as object field not as global instance. Also use default init method.

- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame])
       aDate = [[NSDate date] retain]
    }
    return self;
}
Skie
A: 

I guess the fact that you have a drawRect: method means that you are subclassing NSView or perhaps UIView. In neither case is the init message ever sent to them. They are always initialised with initWithFrame: or initWithCoder: (initWithCoder: is used to instantiate a view from a nib file). You'll need to override both of those methods, or - since you only have one global instance of aDate - you should implement +initialize

+(void) initialize
{
    if (aDate == nil)
    {
        aDate = [[NSDate date] retain];
    }
}
JeremyP