我对Objective-C非常陌生,我试图开发一个iPhone应用程序。我的问题是当我在一个NSTimer中使用一个对象时,我得到了“消息发送到解除分配的实例0x3d54830”的错误。当我不使用NSTimer时,我可以很好地使用该对象。例如:Objective-C对象意外解除分配
//These can be any objects. Le't say I have a Song class and a SongReader class in the header file SongTest.h
Song *song;
SongReader *reader;
NSTimer *timer;
- (void)justDoIt;
//In the implementation file SongTest.m
- (void)viewDidLoad {
reader = [[SongReader alloc] init];
song = [reader readSong];
timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(justDoIt) userInfo:nil repeats:YES];
}
- (void)justDoIt {
NSLog(@"This is a song integer property: %d", song.wordCount);
}
- (void)dealloc {
[reader release];
[super dealloc];
}
的justDoIt方法/选择/消息内的歌曲对象根据调试器已经释放。我究竟做错了什么?即使我这样做:
song = [[reader readSong] retain]; //or
[song retain]; //or
[reader retain];
该对象仍然意外地释放分配。同样,这个问题只发生在我使用NSTimer时。就像在计时器触发之前对象被释放一样。
顺便说一句,请认为没有语法错误。问题是围绕对象释放(运行时)。这不是编译器错误。 – tonex 2010-02-05 14:43:48