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 :)