You'll have to read more about retain, release, and properties. I see you doing things like this:
Code:
//destroy the "location" string, even if someone else is using
//or retaining it.
[location dealloc];
You should not call "dealloc" yourself except [super dealloc] at the end of your own dealloc method. You should be calling [location release] here instead. Same with [format dealloc].
Your setDiaryEntry, setDateString, setLocation methods are also very dangerous because they don't retain the data you send them. It may work now because you call addEntry right away, but If you try to access those pointers later (after someone else releases these strings) they may be invalid.
The more correct way to write them would be:
Code:
-(void)setDiaryEntry:(NSString *)newEntry{
// do nothing if pointer is to the same entry
if (newEntry != diaryEntry){
[diaryEntry release]; //release old entry
diaryEntry = [newEntry retain]; //retain new entry
}
}
A better and easier way to handle this would be to use properties; whatever book you're using should have chapters on retain, release, and properties.
Location: Somewhere the streets are on fire, the sewers are flooded, and the cats are high on catnip
Posts: 527
So I changed my code around
Quote:
Originally Posted by smasher
You'll have to read more about retain, release, and properties. I see you doing things like this:
Code:
//destroy the "location" string, even if someone else is using
//or retaining it.
[location dealloc];
You should not call "dealloc" yourself except [super dealloc] at the end of your own dealloc method. You should be calling [location release] here instead. Same with [format dealloc].
Your setDiaryEntry, setDateString, setLocation methods are also very dangerous because they don't retain the data you send them. It may work now because you call addEntry right away, but If you try to access those pointers later (after someone else releases these strings) they may be invalid.
The more correct way to write them would be:
Code:
-(void)setDiaryEntry:(NSString *)newEntry{
// do nothing if pointer is to the same entry
if (newEntry != diaryEntry){
[diaryEntry release]; //release old entry
diaryEntry = [newEntry retain]; //retain new entry
}
}
A better and easier way to handle this would be to use properties; whatever book you're using should have chapters on retain, release, and properties.
I'm noticing a leak from UITextView when the user clicks on it...
You'll get a leak if you're retaining something and not releasing it when you're done with it. If you used the setDiaryEntry that I wrote above, or you used a property, then you should release newEntry in the dealloc method for that class.