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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:28:19+00:00 2026-05-26T13:28:19+00:00

@implementation TestClass – (id) init { string = [[NSString alloc] init]; return self; }

  • 0
@implementation TestClass

- (id) init
{
    string = [[NSString alloc] init];
    return self;
}

- (void) release
{
    [string release];
}

@end



@implementation MainScreen

- (void) addItems
{
    [myMutableArray addObject:[[[TestClass alloc] init] autorelease]];
}

- (void) release
{
    [myMutableArray release];
}

My Question is when we release the myMutableArray, will it call the release method of TestClass or not?

  • 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-05-26T13:28:20+00:00Added an answer on May 26, 2026 at 1:28 pm

    No, that’s not how Cocoa memory management works. When you add the object to the array, the array expresses that it owns the object by sending the -retain message. The array keeps this ownership until it itself disappears (i.e. is deallocated), then it no longer needs the object and sends it -release to relinquish ownership. The array does not need to retain or release the objects every time it is retained or released.

    To summarise: objects retain other objects when they need to take ownership of them, and release them when they no longer need that ownership.

    This points to your memory management of the string ivar being incorrect. You correctly take ownership of a zero-length string in -init (where I’m using “correctly” in a very loose sense), but then every time your object is released it releases the string. Consider:

    TestClass *obj = [[TestClass alloc] init];
    [obj retain];
    [obj release];
    [obj release];
    

    the above is likely to crash (and if it doesn’t, you’re very unlucky). You should release the string in -dealloc, when your object finally doesn’t need it any longer. If you change the object referred to by the ivar, you also have to change the ownership.

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

Sidebar

Related Questions

template <class T> class Singleton { public: static T& instance() { boost::call_once(init, flag); return
I made this implementation for this problem : http://www.spoj.pl/problems/SHOP/ #include<iostream> #include<stdio.h> #include<queue> #include<conio.h> #include<string.h>
The implementation used where I work uses about 10 columns in the table: CUSTOMFIELD1,
My implementation of Sieve of Atkin either overlooks primes near the limit or composites
What is a good implementation of a IsLeapYear function in VBA? Edit: I ran
I've created an implementation of the QAbstractListModel class in Qt Jambi 4.4 and am
.Net's implementation of HTTP is ... problematic. Beyond some issues in compliance with HTTP/1.0,
Do you know any implementation / technique which would provide similar behavior as Viewstate
Here is my short implementation of Russian Peasant Multiplication . How can it be
I'm writing an implementation of the XXTEA encryption algorithm that works on streams, ie,

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.