lostin2010
lostin2010

Reputation: 143

ARC , stringWithUTF8String memory increase steadily “Live Bytes”

I'm using ARC , each time I call the below code . I find the "Live Bytes" will increase a little every time .there must have memory leak here.

char* example = (char *)sqlite3_column_text(compiledStatement, 1);                                

label.text = [NSString stringWithUTF8String:example];

but if I do in the way below, the memory will keep stable on one number of bytes forever. but the label.text content is not the exact word I want , it's corrupt like "&#(&(*@#)#@$".

char* example = (char *)sqlite3_column_text(compiledStatement, 1);                                

label.text = [NSString stringWithFormat:@"%s",example];

enter image description here

enter image description here

Upvotes: 1

Views: 480

Answers (3)

CouchDeveloper
CouchDeveloper

Reputation: 19116

Old question, but still actual:

It's likely an "autorelease problem" as @jlegakis already pointed out.

I just want to propose a different approach to get rid of the autoreleased returned object from class factory methods like [NSFoo fooWith...]:

Instead, if you use the pattern

NSFoo* foo = [[NSFoo alloc] initWith...]

the created object is not autoreleased. So, if you write:

char* example = (char *)sqlite3_column_text(compiledStatement, 1);
label.text = [[NSString alloc] initWithUTF8String:example];

the "autorelease problem" should be gone.

Upvotes: 1

jlegakis
jlegakis

Reputation: 31

I was having the exact same problem. It turned out to be because stringWithUTF8String was using autorelease internally, so the memory wasn't freed until the end of the enclosing autorelease pool block (which in my case was the entire app).

As an experiment, try this:

@autoreleasepool {
  char* example = (char *)sqlite3_column_text(compiledStatement, 1);                                
  label.text = [NSString stringWithUTF8String:example];
}

and you should see the live bytes stop growing.

Upvotes: 0

Mark McCorkle
Mark McCorkle

Reputation: 9414

Try reading the string this way and see if any change occurs.

char* example = (char *)sqlite3_column_text(compiledStatement, 1);                                

label.text = [NSString stringWithFormat:@"%@",example];

Upvotes: 0

Related Questions