Release pointer value to be used again?
When using the lines below, I receive the difference between the startTimer & stopTimer time-stamps, perfect. But my issue is, I can't seem to release/dealloc the startTimer & stopTimer so that I can use them again as fresh values.. could anyone help?
Basically, in the same view, I'm starting the timer, stopping the timer and then printing the time difference. Then I want to start that whole process again with fresh values.
startTimer = [[NSDate alloc]init];
stopTimer = [[NSDate alloc]i开发者_如何转开发nit];
newTime = [NSString stringWithFormat:@"Time : %f", [stopTimer timeIntervalSinceDate:startTimer]];
I tried using: (but they don't seem to work)
[startTimer dealloc];
[stopTimer dealloc];
[startTimer release];
[stopTimer release];
startTimer = nil;
stopTimer = nil;
When using these, the application crashes the second time I use these.
You should simply call release
to relinquish your interest/ownership on the objects in question, prior to alloc/initing the objects again if so required.
In terms of directly calling dealloc
, you should never do this (apart from when you call [super dealloc];
at the end of any dealloc method your create), as the object in question may still be in use elsewhere. (This obviously wouldn't be possible in the example you provide, but it's a good heuristic to observe.)
To be honest, I'd recommend a thorough read of the Memory Management Programming Guide as this will pay dividends in the future.
You don't need to call dealloc. It's called after the objects get released when their refcount drops to 0.
[In this case - after release
message call]
You can call release after that allocate and init timers, In that case you will have freshed timers.
[startTimer release];
[stopTimer release];
startTimer = [[NSDate alloc]init];
stopTimer = [[NSDate alloc]init];
I think this will help you !
精彩评论