开发者

NSTimer memory error

开发者 https://www.devze.com 2023-03-05 15:58 出处:网络
I am using property. self.refreshTimer = nil; Inthis string I got CFRelease error. Why do I get an error?

I am using property. self.refreshTimer = nil; In this string I got CFRelease error. Why do I get an error?

@property (nonatomic, retain) NSTimer* refreshTimer;

- (id) init
{
    self = [super init];
    if (self != nil) 
    {
        self.refreshTimer = [NSTimer timerWithTimeInterval:600 target:self selector:@selector(timerRefreshGPS:) userInfo:nil repeats:YES];
        [[NSRunLoop currentRunLoop] addTimer:refreshTimer forMode:NSDefaultRunLoopMode];
    }
    return self;
}

-(void) updateUserGPSLocation:(CL开发者_开发问答Location*)newLocation
{
    [refreshTimer invalidate];
    [refreshTimer release];
    self.refreshTimer = nil; 

    self.refreshTimer = [NSTimer timerWithTimeInterval:600 target:self selector:@selector(timerRefreshGPS:) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:refreshTimer forMode:NSDefaultRunLoopMode];
}

- (void)dealloc 
{
    [refreshTimer invalidate];
    [refreshTimer release];
    self.refreshTimer = nil; 
    [super dealloc];
}


self.refreshTimer = nil; 

setting self.refreshTimer = nil, since refreshTimer is a property has the effect of releasing the current value of refreshTimer, then assigning nil to it. You've double-released.

I'm also not certain that you have the references that you think you have there - the form of the allocation you use should return an autoreleased timer, and it will be retained when you install it in the run loop. I don't think you actually own a reference here.

0

精彩评论

暂无评论...
验证码 换一张
取 消