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 6659881
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:05:08+00:00 2026-05-26T02:05:08+00:00

I am confused by the memory management in this scenario. In my app user

  • 0

I am confused by the memory management in this scenario.
In my app user makes periodic input inside UITextField tf and the typed strings (NSString*) are stored as elements of a MSMutableArray *arr through addObject. The stored collection is displayed inside a UITableView. My app can go into bkgr and is periodically awakened by push notifications. As I understand it, the data stored in arr can be lost while my app is non-active and, to preserve it, I need to do archive/restore.
My archive/restore are using

NSUserDefaults*prefs;  
[prefs setObjectForKey:x forKey:key] 

to archive and

[prefs objectForKey:key]

to restore every item of arr.

Question1: I think that to prevent the memory leak I need to do [arr release]
Do I also need to do a release on every object which I have added to arr or, since I did not allocate the NSString for tf, it will be done for me automatically?

Question2: in restore I start with something like arr=[[NSMutableArray alloc] initWithObjects:nil]; before I can read and add archived items back to arr. I think that [prefs objectForKey:key] is released as soon as I leave the scope in which it was read – thus I need something like retain to keep it in arr. Would this schema work in the next archive/restore cycle due to another app deep sleep?
Is there a cleaner way of achieving the same?
Thanks.
Victor

  • 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-05-26T02:05:09+00:00Added an answer on May 26, 2026 at 2:05 am

    Adding objects to an NSArray causes the NSArray to retain each object.

    So in a case where you are instantiating objects, then adding them to an array, those objects do not need to be further retained:

    // saving strings inside an array, then array to the NSUserDefaults
    NSString *string1 = @"My String 1";
    NSString *string2 = @"My String 1";
    NSMutableArray *arr = [[NSMutableArray alloc] initWithCapacity:10];
    [arr addObject:string1];
    [arr addObject:string2];
    
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];;  
    [prefs setObject:arr forKey:@"MyArray"];
    
    [arr release];
    

    Then to restore the entire array from prefs:

    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; 
    NSArray *array = [prefs objectForKey:@"MyArray"];
    

    Alternately, to save strings under separate keys, it would be something like this:

    [prefs setObject:[arr objectAtIndex:0] forKey:@"MyFirstStringKey"];
    [prefs setObject:[arr objectAtIndex:1] forKey:@"MySecondStringKey"];
    

    For the restore, you will also just add the items to the array, no retain required:

    // assuming this time several keys added to an array
    // also note using autoreleased version of array - much easier
    NSMutableArray *arr = [NSMutableArray arrayWithCapacity:10];
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];;  
    [arr addObject:[prefs objectForKey:@"MyFirstStringKey"]];
    [arr addObject:[prefs objectForKey:@"MySecondStringKey"]];
    
    // then assign arr or use it otherwise
    

    Also easier still is to use a non-mutable array and instantiate the array with the list of objects you want to have on the array:

    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];;  
    NSArray *arr = [NSArray arrayWithObjects:[prefs objectForKey:@"MyFirstStringKey"], [prefs objectForKey:@"MySecondStringKey"], nil];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i m little confused on memory management in following scenario -(NSData *)getData{ NSData *TempData
I've confused about memory management of Objective-C. Example: .h file @property(nonatomic,retain) NSString *myString; .m
I'm a little bit confused on how handle memory management when dealing with graphics
So I'm a little confused about Qt's memory management. I was looking at the
I'm getting very confused with memory management in relation to vectors and could do
i'm a little bit confused with memory management in view controllers. Lets say i
Memory management has me confused again -- In my .h file, I have: @property
I am just reading through the Practical Memory Management guide. I am somewhat confused
There are a few concepts about iPhone memory management that have got me confused,
I'm programming an iPhone app and I had a question about memory management in

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.