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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T02:40:27+00:00 2026-06-09T02:40:27+00:00

Following is my code(with some unrelated thing omitted): @implementation HomeSceneController … @synthesize options =

  • 0

Following is my code(with some unrelated thing omitted):

@implementation HomeSceneController

...
@synthesize options = _options;    // _options is a NSArray object with 4 elements

- (id)init
{
    if (self = [super initWithNibName:@"HomeScene" bundle:nil]) {
        _currentOptionIndex = 0;

        // Following code add two key event observations, when up arrow or down arrow key is pressed, the corresponding function will be fired.
        [self addObservation:_KEY_UPARROW_ selector:@selector(UpArrowPressHandler)];
        [self addObservation:_KEY_DOWNARROW_ selector:@selector(DownArrowPressHandler)];

    }

    return self;
}

- (void)loadView {
    [super loadView];
    // init _options
    _options = [NSArray arrayWithObjects:
                _localGameOption,
                _networkGameOption,
                _controlSettingOption,
                _quitOption,
                nil];
    [self selectOption:_localGameOption];
}

....

// in these two functions, _options become nil! I don't know why...
- (void)UpArrowPressHandler {
    if (_currentOptionIndex > 0) {
        [self deselectOption:_options[_currentOptionIndex]];
        _currentOptionIndex--;
        [self selectOption:_options[_currentOptionIndex]];
    }
}

- (void)DownArrowPressHandler {
    if (_currentOptionIndex < 3) {
        [self deselectOption:_options[_currentOptionIndex]];
        _currentOptionIndex++;
        [self selectOption:_options[_currentOptionIndex]];
    }
}

@end

when I press up arrow key, the UpArrowPressHandler function is fired. However, the problem is, the _options array become nil.

Can anyone tell me why and how to fix it?

//===========================================================================================

Additional problem:

In the following program:

import "Deep.h"

@implementation Deep

- (id)init {
    if (self = [super init]) {
        _name = @"Deep";
    }
    return self;
}

- (void)test {
    NSLog(_name);
}

@end

The test method can correctly print “Deep” when I call it somewhere else.

However, according to @ATaylor’s explanation, _name should be released.

So, where is my problem?

  • 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-09T02:40:28+00:00Added an answer on June 9, 2026 at 2:40 am

    That’s because _options is getting assigned an autoreleased object, which gets released once you leave the method it was called from.

    Try assigning it to ‘self.options’, which will (most likely) call ‘retain’ on the object, or call ‘retain’ explicitly.

    Once more in code:
    Either use:

    self.options = [NSArray ...];
    

    Or:

    _options = [[NSArray ...] retain];
    

    Please don’t forget to release your ‘options’, once you’re done with it, either by:

    self.options = nil;
    

    or:

    [_options release];
    

    Please only go for ONE of these options, because otherwise you’ll get weird behaviour with the retain count.

    You see, Apple gives us a number of ‘convenience functions’, which return autoreleased objects, meaning we don’t have to bother with their release.
    As a general rule of thumb:
    Call release for every alloc/retain you call yourself.

    To answer the second question:

    _name = @"Deep";
    

    is an assignment to a variable, equivalent to ‘const char *_name = “Deep”;’ from C.
    There is no need to release that, for the simple reason, that you didn’t create or retain it. (No new, No alloc, no retain, no copy).
    The object will not get autoreleased either, because you didn’t call any sort of method, which would cause the variable to be autoreleased.

    Also, see this answer, which deals with the exact problem.

    Just for clarification, to get a string, there are three types of methods.

    NSString *someString;
    someString = @"MyString"; //No retain, no release, static String.
    someString = [NSString stringWithFormat...]; //Autoreleased object, disappears after the method expires.
    someString = [[NSString alloc] initWithFormat...]; //Alloced object, must be released.
    
    • 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: Some functions: A::A(int i_a) {cout<<int Ctor\n;} //conversion constructor void
Somewhere here on Stack Overflow, I had found the following code some day which
the following code for some bizarre reason gives me a value for each row
We have following code: try { // some code throwing MyException } catch (MyException
I am experiencing a problem with the following code in some versions of Internet
The following code is having some problem with the jQuery. <script type="text/javascript"> $(window).load(function() {
The following code snippet generates some warning messages when compiling: Cluster& Myclass::getCluster(const Point &p)
The following (pseudo-) code induces some behaviour which I dont understand. I have 2
i have the following code which switches some fullscreen-background-images (fadeOut, fadeIn). setInterval(function() { var
I've written the following code to display some data, but the data grid is

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.