Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > NSTask problem...

NSTask problem...
Thread Tools
Forum Regular
Join Date: May 2001
Location: France
Status: Offline
Reply With Quote
Oct 24, 2003, 05:06 PM
 
hi !

I have a simple problem of release pool i can"t fix :( can anybody help me to find where is the retainCount problem ?

look at the comment to see where this method crashes...

-(int)pidForPacket:(Packet*)aPacket {
NSAutoreleasePool *autoreleasePool = [[NSAutoreleasePool alloc] init];

int pid = -1;

NSTask *lsofTask = [[NSTask alloc] init];
NSPipe *lsofPipe = [NSPipe pipe];
NSFileHandle *readHandle = [lsofPipe fileHandleForReading];
NSData *lsofResultData = nil;

// write handle is closed to this process
[lsofTask setStandardOutput:lsofPipe];
[lsofTask setStandardError:[lsofTask standardOutput]];
NSString* protocol = ([aPacket protocol] == IPPROTO_TCP)?@"TCP":@"UDP";
NSString* filter = [[NSString alloc] initWithFormat:@"%@@%@:%d",protocol,[aPacket srcIpAddress],[aPacket srcPort]];
NSArray* args = [[NSArray alloc] initWithObjects:@"-i",filter,/*@"-n",@"-P",@"-T",@"-l",*/@"-t" ,nil];

[lsofTask setArguments:args];
[lsofTask setLaunchPath:@"/usr/sbin/lsof"];

[lsofTask launch];

//read the output of the task ipfw list


NSMutableData* lsofResultDataComplete = [[NSMutableData data] init];
while((lsofResultData = [readHandle availableData]) && [lsofResultData length]) {
[lsofResultDataComplete appendData:lsofResultData];
}

//lsofResultData = [readHandle readDataToEndOfFile]; // there is a risk to fill the buffer ?
[lsofTask waitUntilExit];

//get the pid of the packet
NSString* lsofResultString = [[NSString alloc] initWithData:lsofResultDataComplete encoding:NSASCIIStringEncoding];
NSLog(@"lsofResultString = %@",lsofResultString);
if( ! [lsofResultString isEqualToString:@""] ) {
pid = [lsofResultString intValue];
if( ! pid ) {
NSLog(@"lsof %@ returned : %@",[args componentsJoinedByString:@" "] ,lsofResultString);
NSLog(@"error: QuotasManager: pidForPacket:(Packet*)aPacket : lsof returned an invalid pid for packet : %@, pid has been set to -1 !", aPacket);
}
}
else {
NSLog(@"error: QuotasManager: pidForPacket:(Packet*)aPacket : lsof return a empty string for packet : %@",aPacket);
}

NSLog(@"lsofTask retain count = %d",[lsofTask retainCount]);
NSLog(@"args retain count = %d",[args retainCount]);
NSLog(@"filter retain count = %d",[filter retainCount]);
NSLog(@"lsofResultDataComplete retain count = %d",[lsofResultDataComplete retainCount]);
NSLog(@"lsofResultString retain count = %d",[lsofResultString retainCount]);
NSLog(@"-----------------------------------");


[lsofTask release];
[args release];
[filter release];
//[lsofResultDataComplete release]; // if i release these it crashes... but *I* have alloced them !, i am the owner, don't I ?
//[lsofResultString release];


NSLog(@"args retain count = %d",[args retainCount]);
NSLog(@"filter retain count = %d",[filter retainCount]);
NSLog(@"lsofResultDataComplete retain count = %d",[lsofResultDataComplete retainCount]);
NSLog(@"---------------------------------");

[autoreleasePool release];

return pid;
}


the result of this method is :

2003-10-24 23:59:38.745 MyProgram : lsofResultString = 840
2003-10-24 23:59:38.746 MyProgram : lsofTask retain count = 1
2003-10-24 23:59:38.746 MyProgram : args retain count = 3
2003-10-24 23:59:38.746 MyProgram : filter retain count = 2
2003-10-24 23:59:38.746 MyProgram : lsofResultDataComplete retain count = 1
2003-10-24 23:59:38.747 MyProgram : lsofResultString retain count = 1
-----------------------------------
2003-10-24 23:59:38.749 MyProgram : args retain count = 1
2003-10-24 23:59:38.749 MyProgram : filter retain count = 1
2003-10-24 23:59:38.749 MyProgram : lsofResultDataComplete retain count = 1
2003-10-25 00:02:37.346 MyProgram : lsofResultString retain count = 1
---------------------------------

if i understand correctly, the fact that lsofResultDataComplete and lsofResultString have a retainCount of one is bad since they are never released... but releasing them make my app crash at the [autoreleasePool release]; so they must be autoreleased automagically ????

for me, alloc/init and release are balanced...

please help :)
CarraFix, the traffic shaper for OS X !

Enjoy The [CFx] Community !
http://www.carrafix.com
     
Addicted to MacNN
Join Date: Mar 2000
Location: London, UK
Status: Offline
Reply With Quote
Oct 24, 2003, 05:52 PM
 
You won't actually ever see an object with a retain count of 0, because immediately that happens it will be deallocated. Also note that the objects may have been autoreleased internally for various reasons, so they may only be deallocated once your pool is popped and the autoreleases take effect.


I hope that makes sense. It's not a good idea to look at objects' retain counts, because of how Cocoa works it can confuse the hell out of you. It's better to use the debugging tools provided by Apple to catch leaks.
     
altimac  (op)
Forum Regular
Join Date: May 2001
Location: France
Status: Offline
Reply With Quote
Oct 24, 2003, 08:09 PM
 
ok i'll look at MallocDebug and ObjectAlloc

thanks !
CarraFix, the traffic shaper for OS X !

Enjoy The [CFx] Community !
http://www.carrafix.com
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 07:18 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2