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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T16:46:17+00:00 2026-05-14T16:46:17+00:00

I’m having a few problems with some Objective-C and would appreciate some pointers. So

  • 0

I’m having a few problems with some Objective-C and would appreciate some pointers.

So I have a class MapFileGroup which has the following simple interface (There are other member variables but they aren’t important):

@interface MapFileGroup : NSObject {
 NSMutableArray *mapArray;

}

@property (nonatomic, retain) NSMutableArray *mapArray;

mapArray is @synthesize‘d in the .m file.

It has an init method:

-(MapFileGroup*) init
  {
    self = [super init];
    if (self)
    {
       mapArray = [NSMutableArray arrayWithCapacity: 10];
    }

    return self;
  }

It also has a method for adding a custom object to the array:

-(BOOL) addMapFile:(MapFile*) mapfile
{
if (mapfile == nil) return NO;

mapArray addObject:mapfile];
return YES;
}

The problem I get comes when I want to use this class – obviously due to a misunderstanding of memory management on my part.

In my view controller I declare as follows:

(in the @interface):

MapFileGroup *fullGroupOfMaps;

With @property @property (nonatomic, retain) MapFileGroup *fullGroupOfMaps;

Then in the .m file I have a function called loadMapData that does the following:

MapFileGroup *mapContainer = [[MapFileGroup alloc] init];
// create a predicate that we can use to filter an array

// for all strings ending in .png (case insensitive)
NSPredicate *caseInsensitivePNGFiles =
[NSPredicate predicateWithFormat:@”SELF endswith[c] ‘.png'”];

mapNames = [unfilteredArray filteredArrayUsingPredicate:caseInsensitivePNGFiles];
[mapNames retain];

NSEnumerator * enumerator = [mapNames objectEnumerator];
NSString * currentFileName;
NSString *nameOfMap;
MapFile *mapfile;


while(currentFileName = [enumerator nextObject]) {
  nameOfMap = [currentFileName substringToIndex:[currentFileName length]-4]; //strip the extension

mapfile = [[MapFile alloc] initWithName:nameOfMap];
[mapfile retain];
// add to array
[fullGroupOfMaps addMapFile:mapfile];

}

This seems to work ok (Though I can tell I’ve not got the memory management working properly, I’m still learning Objective-C); however, I have an (IBAction) that interacts with the fullGroupOfMaps later. It calls a method within fullGroupOfMaps, but if I step into the class from that line while debugging, all fullGroupOfMaps‘s objects are now out of scope and I get a crash.

So apologies for the long question and big amount of code, but I guess my main question it:

How should I handle a class with an NSMutableArray as an instance variable? What is the proper way of creating objects to be added to the class so that they don’t get freed before I’m done with them?

Many thanks

  • 1 1 Answer
  • 1 View
  • 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-14T16:46:18+00:00Added an answer on May 14, 2026 at 4:46 pm

    When you call [NSMutableArray arrayWithCapacity:], this actually creates an “autoreleased” array. This means that it will be released automatically at some time in the future. Usually, it will be released at the end of the current run loop.

    What you want to do is create an array that is not autoreleased, because you want to explicitly release it when you’re done with it. To do this, use:

    mapArray = [[NSMutableArray alloc] initWithCapacity: 10];
    

    Don’t forget, you need to release it when you’re done with it. Typically, if an instance variable is initialised in the class’s initialiser, then it should be released in the class’s dealloc method, like this:

    - (void) dealloc
    {
        [mapArray release];
        [super dealloc]; // so that NSObject can clean up itself
    }
    

    Some methods return autoreleased objects as a convenience to you, so that you don’t have to manage the memory yourself (it will be cleaned up automatically). In the case of instance variables, you almost never want them to be cleaned up by automatically.

    You can also prevent an autoreleased object from being automatically released by retaining it again.

    mapArray = [[NSMutableArray arrayWithCapacity:10] retain];
    

    The Cocoa Memory Management Programming Guide has a few simple rules to follow about when you should explicitly release, and when you should explicitly retain. Keep the link bookmarked until the rules have become second nature and you’ll have no troubles with Cocoa Memory Management. It is much simpler than it originally seems (or at least, that was the case for me).

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I have some data like this: 1 2 3 4 5 9 2 6
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but

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.