简体   繁体   中英

How i solving memory leak problem?

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.

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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM