Something's leaking... [NSFileHandle availableData] maybe?
Something's leaking... [NSFileHandle availableData] maybe?
- Subject: Something's leaking... [NSFileHandle availableData] maybe?
- From: Jim Turner <email@hidden>
- Date: Thu, 08 Feb 2007 17:07:09 -0600
- Thread-topic: Something's leaking... [NSFileHandle availableData] maybe?
My program reads from a Keyspan USB-to-serial dongle thingie a raw data
stream coming from a news wire. The process is working fabulously, except
the fact that there's something not getting released properly and I'm very
confused.
I'm opening the the physical device with open() and then turning that
filehandle into an NSFileHandle with
fh = [[NSFileHandle alloc] initWithFileDescriptor:fd closeOnDealloc:YES].
I then tell said filehandle
[fh waitForDataInBackgroundAndNotify];
My observer then does the following when a message is received:
-(void) receivedDataFromPort:(NSNotification *)note
{
if( fileBuffer == nil )
{
fileBuffer = [[NSMutableData alloc] initWithCapacity:2048];
}
NSData *data = [[note object] availableData];
if( data && [data length] )
{
unsigned char dBuffer[[data length]];
bzero( dBuffer, [data length] );
[data getBytes:dBuffer];
unsigned char *cp = dBuffer;
int indexCtr = 0;
BOOL foundEOT = NO;
while( (*cp != 0) && !foundEOT && (indexCtr < [data length]) )
{
indexCtr++;
if( *cp == 0x04 )
{
NSLog( @"found EOT" );
foundEOT = YES;
if( indexCtr == [data length] )
{
[fileBuffer appendData:data];
// Write data buffer
[self writeFileToDisk:fileBuffer forCaptureService:[self
managedObject]];
[fileBuffer release];
fileBuffer = nil;
}
else
{
// mushed files
NSData *lastPartOfFile = [data
subdataWithRange:NSMakeRange( 0, indexCtr )];
NSData *firstPartOfNextFile = [data
subdataWithRange:NSMakeRange( indexCtr, [data length] - indexCtr )];
[fileBuffer appendData:lastPartOfFile];
// Write data buffer
NSString *tgtFile = [self writeFileToDisk:fileBuffer
forCaptureService:[self managedObject]];
[fileBuffer release];
fileBuffer = nil;
if( fileBuffer == nil )
{
NSLog( @"allocating a new fileBuffer for second
file" );
fileBuffer = [[NSMutableData alloc]
initWithCapacity:2048];
}
[fileBuffer appendData:firstPartOfNextFile];
}
}
cp++;
}
// If we didn't find the End-Of-Transmission, append all of the data
contents to the file buffer
if( foundEOT == NO )
{
[fileBuffer appendData:data];
}
}
if( data && [data length] )
{
[[note object] waitForDataInBackgroundAndNotify];
}
}
If I let this program run for a while and run a 'heap -guessNonObjects', I
get the following (trimmed to the important parts):
Process 25575: 1 zone
Zone DefaultMallocZone_0x300000: Overall size: 93276KB; 64939 nodes malloced
for 88397KB (94% of capacity); largest unused: [0x21c04400-4111KB]
Found 1506 ObjC classes in process 25575
-----------------------------------------------------------------------
Zone DefaultMallocZone_0x300000: 64939 nodes (90517984 bytes)
CLASS_NAME COUNT BYTES AVG
========== ===== ===== ===
NSConcreteData 21552 690960 32.1
NSConcreteData[16] 21353 87478784 4096.8
<non-object> 5539 1369872 247.3
NSCFString 4511 150944 33.5
NSCFDictionary 2265 155008 68.4
Clearly the problem is something to do with an NSData object someplace not
getting released. Any ideas? Does [NSFileHandle availableData] return
something that I need to release? I don't think so, but I'm not seeing what
else it could be.
--
Jim Turner
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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