TumblrArchive/TMCache

memory leak while reading from diskcache

skyahead opened this issue · 1 comments

I cache NSData in the diskcache like this:
[[[TMCache sharedCache] diskCache] setObject:cachedData forKey:cacheKey block:^(TMDiskCache *cache, NSString *key, id object, NSURL *fileURL) {}];

And read back like this:
tempData = (NSData *)[[[TMCache sharedCache] diskCache] objectForKey:cacheKey];

The thing is tempData is never freed, and Instruments shows that NSKeyedUnarchiver will call NSData's InitWithCoder which calls an unknown function to allocate memory.

I then tried to define a class like below:

// the .h file
@interface HPCachedData : NSObject
@Property NSString *key;
@Property NSData *value;
@EnD

// the .m file

  • (id)initWithCoder:(NSCoder *)decoder {
    self = [super init];
    if (!self) {
    return nil;
    }

    self.key = [decoder decodeObjectForKey:@"key"];
    self.value = [decoder decodeObjectForKey:@"value"];

    return self;
    }

  • (void)encodeWithCoder:(NSCoder *)encoder {
    [encoder encodeObject:self.key forKey:@"key"];
    [encoder encodeObject:self.value forKey:@"value"];
    }

The the memory is still not freed.

Another issue is in both cases, Instruments show 32 bytes leaks in NSConcreateMutableData.

What did I do wrong?
Thanks!

Why exactly are you expecting it to be freed? Are you clearing out the TMMemoryCache instance?