NSArchive/NSUnarchive and memory leaks
NSArchive/NSUnarchive and memory leaks
- Subject: NSArchive/NSUnarchive and memory leaks
- From: Miguel Morales <email@hidden>
- Date: Tue, 14 Aug 2001 09:13:33 -0700
I'm having a problem with memory leaks with NSArchive and NSUnarchive
that are driving me batty. I can't find any dumb mistakes, and I'm
wondering if there are any subtleties that I have missed in the
documentation that Obj-C mavens may know about.
I have a program where I need to cache some information to disk for a
while, then uncache it and throw it away. So I have NSArrays
(photonArray) of photon objects, which are the world's simplest
objects - they simply contain 4 doubles and an int. The basic process
is to cache an array to disk, keep the name of the file in an array, and
use the name to unpack the object on the far side. The problem is that
I'm getting memory leaks on both legs of this journey (observed in top
and MallocDebug), a small one when I archive, and a large one when I
unarchive. All of my objects are releasing (additional autoreleases for
either the photonArray or the photon objects themselves generate
crashes). Is there anything else I need to do, has anyone else seen
this type of behavior? I'm wondering whether there are file pointers
which are being left open, or any other things I haven't thought of.
Thank you for your help,
-Miguel
Code snippets follow:
//Computing along with an array of photons in photonArray
[NSArchiver archiveRootObject: photonArray toFile: [NSString
stringWithCString:fileName]];
[photonArray release];
//some more work is done then
oldPhotonArray = [NSUnarchiver unarchiveObjectWithFile: [NSString
stringWithCString:fileName]];
//this is (I'm pretty sure) an already autoreleased object, and all
this code is a local autorelease pool
//The encode/decode routines within the photon objects are:
-(void)encodeWithCoder:(NSCoder *)encoder
{
[encoder encodeValueOfObjCType:@encode(double) at:&RA];
[encoder encodeValueOfObjCType:@encode(double) at:&DEC];
[encoder encodeValueOfObjCType:@encode(double) at:&TIME];
[encoder encodeValueOfObjCType:@encode(double) at:&WEIGHT];
[encoder encodeValueOfObjCType:@encode(int) at:&PSF];
return;
};
-(id)initWithCoder:(NSCoder *)decoder
{
[super init];
[decoder decodeValueOfObjCType:@encode(double) at:&RA];
[decoder decodeValueOfObjCType:@encode(double) at:&DEC];
[decoder decodeValueOfObjCType:@encode(double) at:&TIME];
[decoder decodeValueOfObjCType:@encode(double) at:&WEIGHT];
[decoder decodeValueOfObjCType:@encode(int) at:&PSF];
return self;
};
//Where the class definition is:
@interface PhotonClass : NSObject <NSCopying, NSCoding> {
// Variables needed for a photon
double RA; //in seconds
double DEC; //in degrees (zero is the equator as usual)
double TIME; //time of arrival in seconds since Modified Williams Year
zero
double WEIGHT; //Weight given to the photon (used for gamma-hadron
separation),
//units of normalized probability (0-1 range,
percentage type notation)
int PSF; //indicator for the PSF appropriate for this photon
}
//some method declarations
@end