0

I am creating a calculator app and this is my first ios app, so i did not care much about memory management, but now that I look back I see some leaks. I know exactly where the leaks are(used Leaks - XCODE), as all the leaks are caused by NSStrings which cannot be modified. Hence, I have been allocating strings even for changing the text of the label:

label.text = [[NSString alloc] initWithFormat: @"%@", number];

where number is another string(which i have to modify a lot and again causing leaks) Hence, everytime I modify the label, I have a leak.

Can someone please suggest what datatype to use so that I can just use one copy of the object and keep modifying it as I need. OR another way to change the label,number either w/o using alloc or releasing it in time.

I have tried autoreleasing it but it crasehes the app. FYI, along with the leaks, the app works great :)(but i know its wrong, hence)

Please help! Thank You

3 Answers 3

2

Assuming your label is UILabel, then your problem is obvious, the text property of UILabel is defined as retain, when you do an alloc it returns an object with retain +1 when you assign to the property the setter increases the retain count by one, therefore you have a +2 retain count, and when u get rid of your label you leak, the thing should look like

label.text = [[[NSString alloc] initWithFormat: @"%@", number] autorelease]; 

or

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

You should read up on memory managment guidelines here

-Daniel

5
  • I had tried both the answers that you have suggested but it only crashes the app. Is there some other datatype like a string that I can use to modify/edit the variables ?
    – pat
    Oct 28, 2011 at 20:42
  • you have something else wrong in your code...show some more...where does it crash
    – Daniel
    Oct 28, 2011 at 23:35
  • Thanks daniel, I changed all NSStrings to NSMutableStrings and modified the code accordingly! Now everything is working fine and there are no memory leaks now !
    – pat
    Oct 29, 2011 at 4:22
  • I am still not sure why [NSString stringWithFormat: @"%@", number]; was not working, but, I just created one instance of NSMutableString for each of the variables and then kept modifying that one instance till the end and it worked out fine as I had only the one instance of each variable to release.
    – pat
    Oct 30, 2011 at 16:25
  • I knew for sure that I was allocating NSStrings each and every time for each variable as there is now way to modify a NSString. Hence all the leaks were involving NSStrings. I then realized that NSMutable string allows the one instance to be modified and thats exactly what I wanted !
    – pat
    Oct 30, 2011 at 16:29
0

try using

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

For assigning text to a label, use an autoreleased string instead.

By the way... what is number? is it a string or is it an integer?

Later on, you might want to read up on some documentation regarding memory management on the iOS.

3
  • number is a nsstring object. I am also having leakage problems with number in a similar way. I have tried your method and the one suggested above and using either of them makes my app crash.
    – pat
    Oct 28, 2011 at 20:46
  • if it's making your app crash, then you have a bigger problem with just memory management. You might want to post another question with some actual code (i.e. the code that creates a number and then assigns it to the label). Oct 29, 2011 at 0:22
  • Thanks micheal, I changed all NSStrings to NSMutableStrings and modified the code accordingly! Now everything is working fine and there are no memory leaks now !
    – pat
    Oct 29, 2011 at 4:23
0

Use this instead:

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

stringWithFormat creates an autoreleased NSString that will be released for you when the autorelease pool is drained.

4
  • There is something else going on here then. Are you sure 'number' is an NSString? Maybe you should post some more of the code and let us know exactly what the crash output is. Oct 28, 2011 at 20:54
  • Hey, I have written all the code in the appdelegate and had deleted the viewcontroller and the .xib file. I am using the mainwindow.xib file. I dont remember why I did this weird thing, but could this be the issue? Also is it ok if I post my whole code here?
    – pat
    Oct 28, 2011 at 21:24
  • Well, start by posting the crash description that you see in the output window in Xcode and we'll take it from there. Oct 28, 2011 at 21:53
  • Thanks robin, I changed all NSStrings to NSMutableStrings and modified the code accordingly! Now everything is working fine and there are no memory leaks now !
    – pat
    Oct 29, 2011 at 4:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.