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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:50:27+00:00 2026-05-22T21:50:27+00:00

why do I need this autorelease after [NSMutableArray array] to avoid a memory leak?

  • 0

why do I need this autorelease after [NSMutableArray array] to avoid a memory leak?

That is Instruments told me there was a leak. By putting the autorelease in it solved it, however I’m not sure why this would be required. The “array” method wasn’t like an INIT or COPY etc…

@interface Weekend : NSObject {
    NSMutableArray*     _events;     
}
@property (nonatomic, retain) NSMutableArray* events;
@end

@implementation Weekend

@synthesize events = _events;

- (id)init {
    if (self == [super init])
    {
        self.events = [[NSMutableArray array] autorelease];    // WHY IS THIS AUTORELEASE REQUIRED
    }
    return self;
}

- (void) dealloc {
    [_events release];  _events = nil;
    [super dealloc];
}

@end

NOTE: This is what I see in Intruments when I take autorelease out (and after I changed the “if (self == [super init])” to “if ((self = [super init]))”

#  Category     Event        Code Location
0  __NSArrayM   Malloc       at the [NSMutableArray array] point
1  __NSArrayM   Autorelease  at the [NSMutableArray array] point
2  __NSArrayM   Retain       at the @synthesize events = _events; point of the code
3  __NSArrayM   Release      QuartzCore - CA:Transaction::observer_callback(__CF........)
                             (from main.m:14 - "int retVal = UIApplicationMain(argc, argv, nil, nil);")
  • 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-22T21:50:28+00:00Added an answer on May 22, 2026 at 9:50 pm

    Why do you need that extra release? You don’t. Not there, anyway.

    The problem is you’re overretaining _events somewhere else. Maybe you’re passing it to another class that’s retaining without releasing? Leaks are always attributed by Instruments to creation of the object, not the unbalanced retain.

    Adding that autorelease instead of finding the unbalanced retain is the memory management equivalent of having an answer that’s off by 0.3 and just adding 0.3 to fix it. You need to remove that and fix the real problem.

    Edit: After reading your latest edit, I think you’ll probably find that Weekend itself is being leaked.

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

Sidebar

Related Questions

So from this string: name[id] I need this: id I used str.split ('[]'), but
I need help on this following aspx code aspx Code: <asp:Label ID =lblName runat
I need to validate this simple pick list: <select name=<%= key %>> <option value=ETC
I need help with this route map routes.MapRoute(Blog_Archive, Blog/Archive/{year}/{month}/{day}, new { controller = Blog,
I need to know this since this is a pre-req for .NET 3.5 and
I need to shorten this query and while I'm pretty good at SQL, I'm
Maybe the need to do this is a 'design smell' but thinking about another
I have created an web service I need to publish this service. I need
This is different than the simple 2 column layout. I need to have this
Unfortunately, I need to do this. I'm using ELMAH for my error log. Before

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.