Re: File Reading Problems
Re: File Reading Problems
- Subject: Re: File Reading Problems
- From: Ken Thomases <email@hidden>
- Date: Wed, 29 Apr 2009 15:12:15 -0500
On Apr 28, 2009, at 9:19 PM, Pierce Freeman wrote:
- (void)awakeFromNib
{
NSFileHandle *remoteConnection = [NSFileHandle
fileHandleForReadingAtPath:@"/Users/user/Desktop/file.plist"];
The above does not promise to keep the NSFileHandle object around for
as long as you need it. It's not at all clear to me that -
readToEndOfFileInBackgroundAndNotify retains the file handle for its
duration.
So, you need to manage the lifetime of the file handle by retaining it
here and releasing it when you know you're done with it.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(readAllTheData:)
name:NSFileHandleReadToEndOfFileCompletionNotification
object:remoteConnection];
[remoteConnection readToEndOfFileInBackgroundAndNotify];
}
- (void)readAllTheData:(NSNotification *)note {
NSString *errors = nil;
NSData *contentsOfDockFile = [note object];
NSLog(@"%@", contentsOfDockFile);
NSDictionary *testing = [NSPropertyListSerialization
propertyListFromData:contentsOfDockFile
mutabilityOption:NSPropertyListImmutable format:nil
errorDescription:&errors];
NSLog(@"%@", testing);
[[NSNotificationCenter defaultCenter] removeObserver:self
name:NSFileHandleReadToEndOfFileCompletionNotification object:[note
object]];
[testing release];
}
On Apr 28, 2009, at 10:15 PM, Pierce Freeman wrote:
I added [testing retain] after the declaration of
the variable, and I no longer get the wheel of death and my app
freezing up.
This smacks of flailing without understanding.
The problem with your original code is that -propertyListFromData:...
gives you an object, but does not give you the right/responsibility
for releasing it, and yet you were releasing it anyway.
While it is technically correct to solve this by adding [testing
retain], it is redundant. You could have just removed your [testing
release]. The object is guaranteed to live at least until you return
out of your -readAllTheData: method, so you need not retain it. And,
if you don't retain it, you should not release it.
All this is to concur with Adam's suggestion that you reread the
memory management guide.
Regards,
Ken
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden