• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: File Reading Problems
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: File Reading Problems


  • Subject: Re: File Reading Problems
  • From: Pierce Freeman <email@hidden>
  • Date: Wed, 29 Apr 2009 15:58:07 -0700
  • Thread-topic: 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" <email@hidden> 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 (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

References: 
 >Re: File Reading Problems (From: Ken Thomases <email@hidden>)

  • Prev by Date: Re: Core Data Fetches + Transient Properties + NSPredicateEditor = Sadness
  • Next by Date: Re: virtual keycode to character
  • Previous by thread: Re: File Reading Problems
  • Next by thread: Errors occur when using NSOutputStream to transfer big files
  • Index(es):
    • Date
    • Thread