sam18
sam18

Reputation: 641

how to free NSMutableString safely in iOS7

-(void)showLog
{
    NSMutableString* msgStr = nil;

    msgStr = [NSMutableString stringWithFormat:@"%s","Log :"];

    //show Log

    [msgStr release];

}

This is my code. I call this function from a thread frequently. I have disabled ARC and I am testing on iPad Mini with iOS 7 from xcode 5. in XCode 5, its show successive memory allocation. Even though I release this msgStr why does it shows increment in memory allocation?

Any help or suggestion will be great.

Upvotes: 0

Views: 300

Answers (3)

Pawan Rai
Pawan Rai

Reputation: 3444

the point you missing here

[NSMutableString stringWithFormat:@"%@",_someString];

This line create a autorelease nsstring object, because stringWithFormat is a factory method which return a instance variable of type nsstring.

In second line you are releasing it. no need to do this here, it will be released from autorelease pool.

Upvotes: 5

Jenson
Jenson

Reputation: 909

If you want to self release your NSString instance, do it like this:

-(void)showLog
{
    NSMutableString* msgStr = nil;

    msgStr = [[NSMutableString alloc] initWithFormat:@"%s", "Log :"];

    //show Log

    [msgStr release];
}

If you don't change msgStr use an immutable string type:

-(void)showLog
{
    NSString* msgStr = [[NSString alloc] initWithFormat:@"%s", "Log :"];

    //show Log

    [msgStr release];
}

Upvotes: 0

Volker
Volker

Reputation: 4660

stringWithFormat: returns an autoreleased NSString, so you are overreleasing it. It is not leaking. Memory allocations will happen all the time and are only a problem if not released anymore or if leaking.

How do you check for memory allocations? Are you using Instruments?

Upvotes: 2

Related Questions