开发者

Objective-C NSString Transformable attribute memory leak

开发者 https://www.devze.com 2023-01-07 22:46 出处:网络
I have a CoreData NSManagedObject subclass, TextNarration, that has an attribute of type NSString, textURI. That class implements the NSCoding protocol methods: initWithCoder and encodeWithCoder this

I have a CoreData NSManagedObject subclass, TextNarration, that has an attribute of type NSString, textURI. That class implements the NSCoding protocol methods: initWithCoder and encodeWithCoder this way:

    - (id) initWithCoder: (NSCoder *)coder
{
    if (self = [super init])
    {
        self.textURI = [coder decodeObjectForKey:@"textURI"];
    }
    return self开发者_JAVA百科;
}

- (void) encodeWithCoder: (NSCoder *)coder
{
    [coder encodeObject:textURI forKey:@"textURI"];

}

When I create a new Instance I do something like this:

TextNarration * textNarr = [[TextNarration alloc] init];
NSString * tURI = [[NSString alloc] initWithString:[dictionaryAction objectForKey:@"narrationURI"]];
//NSString * tURI = [[NSString alloc] initWithString:@"aspa"];
textNarr.textURI = tURI;
[action_collected addObject:textNarr];
[tURI release];
[textNarr release];

Where action_collected is an NSArray I'm going to put as transformable attribute in an entity in my model, and [dictionaryAction objectForKey:@"narrationURI"] is an NSString previously autoreleased in another context. How come I get a memory leak on that string and none if a use the commented line instead? Looking at instruments it appears that the autoreleased string receives a retain from initWithString (and encodeWithCoder is called twice)


The answer is simple, that class is not a subclass of NSManagedObject and thus it requires an appropriate dealloc method which releases the attributes. Clumsy me for not noticing in time

0

精彩评论

暂无评论...
验证码 换一张
取 消