• 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: NSTask Memory Problems
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSTask Memory Problems


  • Subject: Re: NSTask Memory Problems
  • From: j o a r <email@hidden>
  • Date: Fri, 23 Jan 2004 23:59:37 +0100

On 2004-01-23, at 23.10, Owen Anderson wrote:

> Alright, I acted on the suggestions I got and modified the code like
> such. However, it STILL SIGBUSs

Where does it crash? Launch it in the debugger, and find the offending
line of code.

> NSString* tmpString = [[NSString alloc] init];

This is completely pointless, as the string is immutable. It is also a
memory leak.
Alternatives are:
NSString *tmpString = [NSString string];
NSString *tmpString = @"";
But, I have a better alternative in the code snippet below, read on...

> NSTask* search = [[NSTask alloc] init];
<snip>
> NSMutableArray* args = [[NSMutableArray alloc] init];

Why not autorelease these two right away?
NSMutableArray *args = [[[NSTask alloc] init] autorelease];
NSMutableArray *args = [NSMutableArray array];

> [search launch];

Add: [search waitUntilExit]; here, then do something like this:

NSData *data = [[[search standardOutput] fileHandleForReading]
availableData];
NSString *tmpString = nil;
if ((data != nil) && ([data length] > 0))
{
tmpString = [[[NSString alloc] initWithData:data
encoding:NSUTF8StringEncoding] autorelease];
}

But note, that your original code, and the one above, will only work if
you *know* that the output fits in the buffer of the NSPipe - and
that's not something I would suggest that you bet on. The documentation
and the list archives are full of sample code that you can use to
collect data from the file handle while waiting for the task to run to
completion.

BTW., do you know that the task is returning data in UTF-8 format?

j o a r

[demime 0.98b removed an attachment of type application/pkcs7-signature which had a name of smime.p7s]
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

References: 
 >NSTask Memory Problems (From: Owen Anderson <email@hidden>)
 >Re: NSTask Memory Problems (From: Daryn <email@hidden>)
 >Re: NSTask Memory Problems (From: Owen Anderson <email@hidden>)

  • Prev by Date: NSPopUpButtonWillPopUpNotification
  • Next by Date: Re: cocoa-dev digest, Vol 2 #3520 - 11 msgs
  • Previous by thread: Re: NSTask Memory Problems
  • Next by thread: Re: NSTask Memory Problems
  • Index(es):
    • Date
    • Thread