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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:55:39+00:00 2026-06-02T07:55:39+00:00

Although experienced with OOP, I am an absolute newbie with Objective-C. I have the

  • 0

Although experienced with OOP, I am an absolute newbie with Objective-C. I have the following code:

// header files have been imported before this statement...
CCSprite *treeObstacle;
NSMutableArray *treeObstacles;

@implementation HelloWorldLayer {
}

-(id) init
{
    // create and initialize our seeker sprite, and add it to this layer
    treeObstacles = [NSMutableArray arrayWithObjects: nil];        
    for (int i=0; i<5; i++) {
        treeObstacle = [CCSprite spriteWithFile: @"Icon.png"];
        treeObstacle.position = ccp( 450-i*20, 100+i*20 );
        [self addChild:treeObstacle];
        [treeObstacles addObject: treeObstacle];
    }
    NSLog (@"Number of elements in array = %i", [treeObstacles count]);
    return self;
}

- (void) mymethod:(int)i {
    NSLog (@"Number of elements in array = %i", [treeObstacles count]);
}

@end

The first NSLog() statement returns “Number of elements in array = 5”. The problem is that (although treeObstacles is a file-scope variable) when calling the method “mymethod”, I’ll get an EXC_BAD_ACCESS exception.

Can anybody please help me?

Thanks a lot
Christian

  • 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-02T07:55:41+00:00Added an answer on June 2, 2026 at 7:55 am

    you created treeObstacles by

    treeObstacles = [NSMutableArray arrayWithObjects: nil];
    

    which will return an autoreleased object, and you didn’t retain it so it will be released soon

    you have to retain it by calling retain on it

    [treeObstacles retain];
    

    of simple create it by

    treeObstacles = [[NSMutableArray alloc] init];
    

    and you need to remember to release it when done like

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

    you need to read more about management in Objective-C
    https://developer.apple.com/library/mac/#documentation/General/Conceptual/DevPedia-CocoaCore/MemoryManagement.html

    or use ARC so no need to worry retain/release anymore
    http://developer.apple.com/library/ios/#releasenotes/ObjectiveC/RN-TransitioningToARC/Introduction/Introduction.html


    another problem, you need to call [super init] in your init method

    - (id)init {
        self = [super init];
        if (self) {
            // your initialize code
        }
    }
    

    otherwise your object won’t initialize properly

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

Sidebar

Related Questions

I have been using the STOMP protocol in various guises. I have experienced this
I am a total newbie in servers/hosting etc, although I have some experience in
Although I have read the similar questions, this problem seems to be the exact
Although somewhat related to this question , I have what I think is a
Although I don't have an iPhone to test this out, my colleague told me
I'm quite experienced in PHP but I don't quite use mod_rewrite (although I should).
I'm kind of new to WPF although I have some experience in Forms, and
I'm new to Python and web development (although I have development experience with Client/Server
Although I do understand the serious implications of playing with this function (or at
For those experienced with C, this will be a simple memory allocation/referencing problem: Here

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.