Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8355891
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T10:00:02+00:00 2026-06-09T10:00:02+00:00

I have the following code: NSString *Items[91]; in the .m file above all the

  • 0

I have the following code:

NSString *Items[91];

in the .m file above all the methods to serve as global array which in my init method i do:

for (j1 = 0; j1 <= 90; j1++)
    {
        Items[j1] = [[NSString alloc] initWithFormat:@""];
    }   

and at some point a different method AA is triggered and do:

Items[40] = [NSString stringWithFormat:@"40. Pers:%g each", PersExemptions];
 Items[41] =@"blah blah";

… etc

and at some point a different method BB is triggered and i see that for Items[40] it says freed object, it’s losing the value it had which defeat the purpose. Grr.

I would like Items array to keep their modified values thru the app until the end and I assumed that using the initWithFormat that i used in the init method should take care of it.
I understand that Items is c-style array (and to convert to NSMutable array would pain) if that’s the problem to begin with.

I appreciate any help on this.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T10:00:04+00:00Added an answer on June 9, 2026 at 10:00 am

    EDIT: As @dasblinkenlight points out, I’m assuming here you are not using ARC.

    You’re using a C-style array here, an array of NSString*.

    The problem is that when you do this…

    [NSString stringWithFormat:@"40. Pers:%g each", PersExemptions]

    stringWithFormat: returns an NSString that you do not own. If you want to hold onto that NSString in your array, you need to retain it. Or you can use the alloc/initWithString that you did earlier:

    Items[40] = [[NSString alloc] initWithFormat:@"40. Pers:%g each", PersExemptions];

    That gives you an NSString that you do own. But of course, that leads to another bug, which is that you just leaked whatever NSString happened to be in Items[40] a moment ago.

    So you could make sure to always release the previous string each time, or you can save yourself a lot of effort and just use an NSMutableArray instead. NSMutableArray will take care of retaining each new value and releasing the one that was just replaced. I know you say that’s too much effort, and without looking at your code no one else can say, but accounting for each object you store might actually be more effort.

    I hope that helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: .h NSString *mainString; .m case 0: case 1: case
I have the following code to open google maps: NSString *urlString = [NSString stringWithFormat:@http://maps.google.com/maps?q=%@,
Hello I have the following code: if(_deviceSegmentCntrl.selectedSegmentIndex == 0) { deviceOne = [[NSString alloc]
I have following code for loading image from url in xml parsing endElement method
I have the following code: NSString *subtitle = [[[node elementsForName:@subtitle] objectAtIndex:0] stringValue]; NSString *duration
I have the following code: NSString *text = @http://bit.ly/111 http://bit.ly/222 http://www.www.www; NSRegularExpression *aLinkRegex =
How to add news via FBConnect? I have the following code: NSString *newsBody =
I have the following code fragment: NSString* value = @value; NSString* key = @key;
I have the following code: - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super
I have following code - methods to read XML files. But it works very

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.