tags:

views:

38

answers:

2

Hi, i developing an application in which i found memory leak in following method how i remove leak?

- (id)initWithString:(NSString *)str attributes:(NSDictionary *)attributes
{

    if ((self = [super init]))
    {
        _buffer = [str mutableCopy];
        _attributes = [NSMutableArray arrayWithObjects:[ZAttributeRun attributeRunWithIndex:0 attributes:attributes], nil];
    }

    return self;

}

I founding leak near this line "_buffer = [str mutableCopy];"

In allocation stack trace i finding simultaneous memory allocation increasing as a CFString.

Thanks.

+1  A: 

mutableCopy retains the returned object, so it is your responsibility to release it when you're done with it. This is in line with the Memory Management Rules.

Echelon
+2  A: 

I think you will not have a memory leak if you put a line [_buffer release] in dealloc method. You have an allocation because for every methods that contains stuff like alloc, retain and copy... you create a new object instance. And that's ok in this case.

Another thing you have to worry is a memory crash of _attributes object. You own an autoreleased object and the next time you try to use it, it may be deallocated already

vodkhang