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

  • Home
  • SEARCH
  • 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 156283
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T10:18:14+00:00 2026-05-11T10:18:14+00:00

I’m developing on the iPhone and I’m missing something about storing objects in an

  • 0

I’m developing on the iPhone and I’m missing something about storing objects in an NSMutableArray – what am I doing wrong and how should I approach this?

The relevant sections of the code are

I have an object, Entity, defined which inherits directly from NSObject. In my class GLView I wish to define an array of these – so GLView.h

@interface GLView : UIView {     BOOL controllerSetup;      // Entity etc - first development task it to get one up     NSMutableArray *arrEntities; } @property(nonatomic, assign) NSMutableArray *arrEntities; 

and in the body in the drawView function – which is called multiple times by a timer event – I allocate the array and, for initial development, just one Entity object and add it to the array

- (void)drawView {     if(!controllerSetup) { // initialize on first call         [controller setupView:self];         controllerSetup = YES;          arrEntities = [NSMutableArray array];          Entity* thisEntity = [[Entity alloc] initEntity];            [arrEntities addObject:thisEntity];     }     Entity *entity = [arrEntities objectAtIndex:0];     [entity tickEntity:0.1]; } 

My understanding was that on the first call of drawView the NSMutableArray would be set up and the instance of Entity added to it, then in the body the a pointer to first copy of entity stored in the array retrieved and this manipulated (obviously I intend to add more and loop over them later).

However what happens it that on the first call of the procedure everything works fine, the objectAtIndex retrieves the entity pointer and all works. However on the second call when controllerSetup is true and the initialization clause not executed arrEntities is empty and the program crashes as objectAtIndex retrieves nil.

So what am I doing wrong? Why does my stored Entity object not persist in the NSMutableArray?

  • 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. 2026-05-11T10:18:15+00:00Added an answer on May 11, 2026 at 10:18 am

    Try replacing the line you create arrEntities with the following:

    arrEntities = [[NSMutableArray alloc] init]; 

    Then make sure to add it to a dealloc method:

    - (void)dealloc {      [arrEntities release];      [super dealloc]; } 

    Basically, because you are not explicity allocating your array (you are using the static +array method), it is being autoreleased. Your program crashes when the array has been autoreleased and your code tries to access it.

    One more thing:

    Entity* thisEntity = [[Entity alloc] initEntity];    [arrEntities addObject:thisEntity]; [thisEntity release]; // add this 

    If you don’t release thisEntity, it will leak. addObject retains the passed object.

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

Sidebar

Ask A Question

Stats

  • Questions 74k
  • Answers 74k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer So after a couple of illfated attempts at trying to… May 11, 2026 at 2:26 pm
  • added an answer Yes, it's definitely possible. The way we do it is… May 11, 2026 at 2:26 pm
  • added an answer If you control one or both: edit one to change… May 11, 2026 at 2:26 pm

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.