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
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
or
You should read up on memory managment guidelines here
-Daniel