Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > NSString memory issue

NSString memory issue
Thread Tools
Fresh-Faced Recruit
Join Date: Jul 2009
Status: Offline
Reply With Quote
Jul 28, 2009, 05:41 AM
 
Hello I'm totally beginner in programming for mac. I was looking for some answers in internet but none helped me to solve my problem, so I'm writing desperately for help

I have following problem: in loop I need to ad some characters to string.

My question is how to do that? Because by doing so:

Code:
NSString *hlp = [[NSString alloc] initWithFormat: $"some string"]; for (int i =0; i< n; i++) {
NSString *sufix = [[NSString alloc] initWithFormat: @"sufix no: %d,", i]; NSString hlp1 = [hlp stringByAppendingString: sufix]; [hlp release]; hlp = hlp1;
} textField.text = hlp;
i got message that "Program recieved signal : EXC_BAD_ACCESS".

Any help would be appreciated.
Doro
     
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Jul 28, 2009, 10:35 AM
 
initWithFormat: returns a string you own. stringByAppendingString: returns a string you do not own. Releasing one is necessary; releasing the other one is wrong. So when you do hlp=hlp1, the next time through the loop it will incorrectly release the string. See the Memory Management Programming Guide for everything you need to know on the topic — it's really a must-read if you want to use Cocoa.
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 06:51 AM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2