retain property points to wrong address
I have singleton class with ivar named formatter. There is some code:
@interface SettingsController : NSObject {
NSDateFormatter *formatter;
}
@property (nonatomic,retain,readwrite) NSDateFormatter *formatter;
@implementation SettingsController
@synthesize formatter,;
SYNTHESIZE_SINGLETON_FOR_CLASS(SettingsController);
-(id)init
{
if ( (self = [super init]) )
{
...
self.formatter = [[[NSDateFormatter alloc] init] autorelease];
...
}
Problem is that sometime when I try to get this formatter that how:
[[SettingsController sharedSettingsController] formatter]
The value is missed. There NSDate,NSString or even my own class instance at address that formatter points.
I've try use [[NSDateFormatter alloc] init] but only [[[NSDateFormatter alloc] init] retain] make problem disappear.
How could it be, when I use @property (nonatomic,retain,readwrite) and never reset this ivar to another value?
EDITE: A开发者_JS百科fter I made subclass for NSDateFormatter, I've clearly and easy seen the release that make fall app. Thanks a lot to progrmr!
You write about class SettingsController
, but are using class SyncProgress
. Which is it?
If you properly @synthesize
d the @property
, what you did in the init
method you show should be correct, assuming you don't release the property -- or at least the same object, from whatever reference to it you may have -- later on, either in the same method or elsewhere. In other words, don't release an autoreleased item.
FWIW, the article that describes the macro says that you should declare
+ (SettingsController *) sharedSettingsController;
I can't see you doing that, so did you?
And you are using class SyncProgress
, not SettingsController
. Assuming you declared that as singleton too, you should do the same for SyncProgress
:
+ (SyncProgress *) sharedSyncProgress;
What is the init
method for SyncProgress
? Do you set the formatter
there too, and does it even have one? Or are you just looking at the wrong class?
精彩评论