我正在项目上运行Leaks,它正在查找与以下代码相关联的NSMutableDictionary内存泄漏,它标记为负责框架:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
Gallery *gallery = [self.galleries.galleries objectAtIndex:indexPath.row];
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:gallery.name forKey:@"name"];
NSNotification *notification = [NSNotification notificationWithName:REQUEST_VIEW_GALLERY
object:self
userInfo:userInfo];
[self post:notification];
}
首先,NSDictionary没有分配,但是通过NSDictionary :: dictionaryWithObject:forKeys:获得了自动释放,其次它是一个不可变的字典,所以怎么会有漏洞呢?
以下是图库核心数据实体的来源:
#import "Gallery.h"
#import "Galleries.h"
#import "Image.h"
@implementation Gallery
@dynamic name;
@dynamic galleries;
@dynamic images;
//------------------------------------------------------------------------------
// Initialisation
//------------------------------------------------------------------------------
+(Gallery *)initWithName:(NSString *)name photos:(NSArray *)photos managedObjectContext:(NSManagedObjectContext *)managedObjectContext
{
NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
request.entity = [NSEntityDescription entityForName:@"Gallery" inManagedObjectContext:managedObjectContext];
request.predicate = [NSPredicate predicateWithFormat:@"name = %@", name];
NSError *error = nil;
Gallery *gallery = [[managedObjectContext executeFetchRequest:request error:&error] lastObject];
if(!gallery && !error)
{
gallery = [NSEntityDescription insertNewObjectForEntityForName:@"Gallery"
inManagedObjectContext:managedObjectContext];
gallery.name = name;
}
for (NSDictionary *photo in photos)
{
Image *image = [Image initWithFlickrData:photo managedObjectContext:managedObjectContext];
[gallery addImagesObject:image];
}
return gallery;
}
//------------------------------------------------------------------------------
// Public
//------------------------------------------------------------------------------
- (void)addImages:(NSOrderedSet *)values
{
self.images = values;
}
- (void)addImagesObject:(Image *)value
{
NSMutableOrderedSet *mutableImages = [NSMutableOrderedSet orderedSetWithOrderedSet:self.images];
[mutableImages addObject:value];
self.images = [NSOrderedSet orderedSetWithOrderedSet:mutableImages];
}
- (void)removeImagesObject:(Image *)value
{
NSMutableOrderedSet *mutableImages = [NSMutableOrderedSet orderedSetWithOrderedSet:self.images];
[mutableImages removeObject:value];
self.images = [NSOrderedSet orderedSetWithOrderedSet:mutableImages];
[self.managedObjectContext deleteObject:value];
}
@end