Re: File Reading Problems
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 (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: File Reading Problems
Hey Ken: Your advice is really appreciated, and I will definitely look over the Memory Management Guidelines when I have a bit more time in my day. ;) On 4/29/09 1:12 PM, Ken Thomases k...@codeweavers.com wrote: 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 (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
File Reading Problems
Hi everyone. I have a small problem with readToEndOfFileInBackgroundAndNotify which is that I can't seem to get it to work right. ;) I am seeding the connection with the file name, and everything looks right except that I keep getting NSConcreteFileHandle: 0x1abfd0 and *** -[NSConcreteFileHandle length]: unrecognized selector sent to instance 0x1abfd0 when attempting to use it. My code is as follows. - (void)awakeFromNib { NSFileHandle *remoteConnection = [NSFileHandle fileHandleForReadingAtPath:@/Users/user/Desktop/file.plist]; [[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]; } Thanks for any help. ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: File Reading Problems
On Tue, Apr 28, 2009 at 9:19 PM, Pierce Freeman piercefreema...@comcast.net wrote: - (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]; } [note object] is your NSFileHandle. You want [[note userInfo] objectForKey:NSFileHandleNotificationDataItem]]. ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: File Reading Problems
Thanks for your help. I don't get those strange errors anymore, however no if I try to save the NSDIctionary to a file (NSDictionary's writeToFile), my app seems to freeze up and in the Debugger it says: Program loaded. sharedlibrary apply-load-rules all Attaching to program: `/Users/user/Desktop/location', process 3361. (gdb) On 4/28/09 7:35 PM, Stephen J. Butler stephen.but...@gmail.com wrote: On Tue, Apr 28, 2009 at 9:19 PM, Pierce Freeman piercefreema...@comcast.net wrote: - (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]; } [note object] is your NSFileHandle. You want [[note userInfo] objectForKey:NSFileHandleNotificationDataItem]]. ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: File Reading Problems
On Apr 28, 2009, at 7:50 PM, Pierce Freeman wrote: Thanks for your help. I don't get those strange errors anymore, however no if I try to save the NSDIctionary to a file (NSDictionary's writeToFile), You're releasing a variable that you don't own (testing). See the Cocoa memory ownership rules at http://developer.apple.com/DOCUMENTATION/Cocoa/Conceptual/MemoryMgmt/MemoryMgmt.html . Why are you using NSFileHandle asynchronous reading for a plist file, anyway? This is unusual, to say the least. -- adam On 4/28/09 7:35 PM, Stephen J. Butler stephen.but...@gmail.com wrote: On Tue, Apr 28, 2009 at 9:19 PM, Pierce Freeman piercefreema...@comcast.net wrote: - (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]; } [note object] is your NSFileHandle. You want [[note userInfo] objectForKey:NSFileHandleNotificationDataItem]]. ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/amaxwell%40mac.com This email sent to amaxw...@mac.com smime.p7s Description: S/MIME cryptographic signature ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: File Reading Problems
Hey Adam: Thanks for your reply. I added [testing retain] after the declaration of the variable, and I no longer get the wheel of death and my app freezing up. In response to your comment about why I am using NSFileHandle, it is because in the final version of this app I will need to scan several hundred plist files and I would like the app to stay responsive during this process. I am aware that I could just use threading for this, but this seemed a less hassle way to accomplish the same thing. On 4/28/09 8:03 PM, Adam R. Maxwell amaxw...@mac.com wrote: On Apr 28, 2009, at 7:50 PM, Pierce Freeman wrote: Thanks for your help. I don't get those strange errors anymore, however no if I try to save the NSDIctionary to a file (NSDictionary's writeToFile), You're releasing a variable that you don't own (testing). See the Cocoa memory ownership rules at http://developer.apple.com/DOCUMENTATION/Cocoa/Conceptual/MemoryMgmt/MemoryMgm t.html . Why are you using NSFileHandle asynchronous reading for a plist file, anyway? This is unusual, to say the least. ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com