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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T10:11:54+00:00 2026-06-05T10:11:54+00:00

I have a simple method in my model to create a NSDictionary object containing

  • 0

I have a simple method in my model to create a NSDictionary object containing its properties.
Unfortunately this method is seen by “Analyse” to be leaking memory :

Potential memory leak of an object allocated on line 76 (marked here with a dot) and stored in ‘dic’.

-(NSDictionary*) getDictionary {
    NSDictionary *dic = [[NSDictionary alloc] init];

    [dic setValue:(id)self.internal_code forKey:@"internal_code"];
    [dic setValue:(id)self.identifier forKey:@"id"];
    [dic setValue:(id)self.owner forKey:@"owner"];
    [dic setValue:(id)self.address forKey:@"address"];
    [dic setValue:(id)self.displayed_name forKey:@"displayed_name"];

    return dic;
}

I am not using ARC.

PS : To people coming in, the original code I posted was correct — it had an autorelease. I edited it after so the memory leak would reappear and to ask precisely why.

  • 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-05T10:11:55+00:00Added an answer on June 5, 2026 at 10:11 am

    When returning an object from a method that doesn’t begin with alloc, copy, mutableCopy or new, that object must be returned as autoreleased.

    More conceptually, it should not be owned by your code when you return it. You take ownership of an object when you type alloc, copy, mutableCopy or new. You relinquish ownership when you type release or autorelease.

    You can either change your return statement to:

    return [dic autorelease];
    

    Or better is to keep the alloc/init/autorelease all on one line so the code is easier to review, and the alloc and release cannot become separated by accident while copy and pasting code:

    NSDictionary *dic = [[[NSDictionary alloc] init] autorelease];
    

    An even easier way is to use this convenience constructor on NSDictionary:

    NSDictionary *dic = [NSDictionary dictionary];
    

    The above lines will fix the memory leak. However, you are also trying to mutate an immutable type (NSDictionary). You should be using a mutable dictionary instead:

    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    

    Finally, you should ideally be setting values with the setObject:forKey: method, although setValue:forKey: will also work.

    For more information on memory management, read the Advanced Memory Management Programming Guide.

    If you are targetting iOS 4 or later, I would highly recommend using ARC.

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

Sidebar

Related Questions

Say I have this simple method: public IEnumerable<uint> GetNumbers() { uint n = 0;
Hi I have this simple method which gets triggered when i click inside a
I've have this DB Model http://www.dropmocks.com/mBgqjs and I'm using CakePHP to build a simple
I have a simple object relation, I wish to persist with hibernate. Basically, its
I am simply learing Ajax with jQuery and have a simple page method that
I was wondering if anyone knew why jQuery doesn't have a simple $().id() method.
I have only simple data types in method signature of service (such as int,
I have a simple mapview that has the following viewdidload method and didupdate to
Say I have a simple client/server scenario with one method: // client code $client
I have a simple jigsaw puzzle i'm making. I have a method that 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.