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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T12:04:57+00:00 2026-06-07T12:04:57+00:00

I have the following variable defined: @property (nonatomic, retain) NSMutableArray *arraySpeechSentences; And I am

  • 0

I have the following variable defined:

@property (nonatomic, retain) NSMutableArray *arraySpeechSentences;

And I am trying to initialise it in the following way:

// Set the array of sentences to the stored array
NSMutableArray *speechSentences = [[NSMutableArray alloc] initWithArray:[tempDict objectForKey:key]];
arraySpeechSentences = speechSentences;
[speechSentences release];

When I try to call [arraySpeechSentences count] the application crashes. However, if I set the variable in the following way:

// Set the array of sentences to the stored array
NSMutableArray *speechSentences = [[NSMutableArray alloc] initWithArray:[tempDict objectForKey:key]];
self.arraySpeechSentences = speechSentences;
[speechSentences release];

I can call [arraySpeechSentences count] perfectly fine. I was under the impression that if you use self. it simply checks to see if variable is already set, and if so it will release the object before assigning it the new value. Have I got this wrong, and if so when should I be using self. to set values?

Thanks for any help,
Elliott

  • 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-07T12:04:59+00:00Added an answer on June 7, 2026 at 12:04 pm

    I’ll try to give a detail answer for this.

    First when you use @property/@synthesize directive you create getter and setter methods around a variable.

    In your case, the variable is called arraySpeechSentences (the compiler will create the variable for you) and you can access these methods (setters and getters) with self..

    self.arraySpeechSentences = // something 
    

    is the same as

    [self setArraySpeechSentences:something]; // setter
    

    And

    NSMutableArray* something = self.arraySpeechSentences;
    

    is equal to

    NSMutableArray* something = [self arraySpeechSentences]; // getter
    

    In the first snippet of code

    NSMutableArray *speechSentences = [[NSMutableArray alloc] initWithArray:[tempDict objectForKey:key]];
    arraySpeechSentences = speechSentences;
    

    arraySpeechSentences points to the same object speechSentences points to. But when you do [speechSentences release] you dealloc that object and now arraySpeechSentences is a dangling pointer. You receive a message sent to a deallocated instance I suppose. Try to enable Zombie to see it.

    Speaking in terms of retain count, the array has a retain count of 1 when you do alloc-init.
    But when you release it, the retain count goes to zero, the object doesn’t exist anymore and you have a crash when you try to access arraySpeechSentences.

    Instead, when you deal with properties, the policy applied to a variable is important. Since the property use a retain policy, when you set an object

    self.arraySpeechSentences = // something
    

    the retain count for the referenced object is increased. Under the hood, saying self.arraySpeechSentences = // something is equal to call the setter like

    - (void)setArraySpeechSentences:(NSMutableArray*)newValue
    {
        // pseudo code here...
        if(newValue != arraySpeechSentences) {
    
           [arraySpeechSentences release];
           arraySpeechSentences = [newValue retain]; 
        }
    }
    

    The second snippet work since the retain count for your object is one when you do alloc-init, becomes two when you call self.arraySpeechSentences = and returns to one when you do the release. This time, the object is maintained alive since it has a retain count of 1.

    If you have a property with a retain or copy policy, don’t forget to release the object in dealloc like, otherwise you can have leaks.

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

    To understand how Memory works I suggest to read MemoryManagement Apple doc.

    P.S. Starting from iOS 5 there is a new compiler feature, called ARC (Automatic Reference Counting), that allows you to forget about retain/release calls. In addition, since it forces you to think in terms of object graphs, I suggest you to take a look into.

    Hope that helps.

    • 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 to set a userId variable: (userId set in prior
I have an NSUInteger defined as a property like so: @property (nonatomic, assign) NSUInteger
There's probably a silly error in my code. I have defined the following variables:
I have the following variable: pageID = 7 I'd like to increment this number
I have the following variable: $argument = 'blue widget'; Which I pass in the
I have the following variable that accepts a file name: var xtr = new
I have the following table variable in SQL Server 2005: DECLARE @results TABLE (id
I have the following global in variable in file_1.pl, in package main: package main;
I have the following string as a variable: var name1 = '001-C6AL7_deepgreen_C3AE7_tankhakinavy_FJAG7_blackredmulti_FBAE7_tanbrown.jpg'; Which is
I have the following string assigned to a variable: $date = 16 June, 2012,

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.