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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T12:33:39+00:00 2026-05-20T12:33:39+00:00

Leak: + ( myDetails* ) initEmptySlideDetails { //ok myDetails* theObject = [[myDetails alloc] init];

  • 0

Leak:

 + ( myDetails* ) initEmptySlideDetails { //ok
    myDetails* theObject = [[myDetails alloc] init];
    theObject.ID = 0;
    return theObject;
}

Question 1: Is this the right way to return an object that I also want to release to avoid memory leak?

No leak:

+ ( myDetails* ) initEmptySlideDetails { //ok
        myDetails* theObject = [[myDetails alloc] init];
        theObject.ID = 0;
        return [theObject autorelease];
    }

Question2: When I use the object, do I need to force a retain? (because of the autorelease).

myDetails* myDetails = [myDetails initEmptySlideDetails];

Hope i’m clear… 🙂

  • 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-20T12:33:40+00:00Added an answer on May 20, 2026 at 12:33 pm

    The reason that you’re getting warned for a leak in the first example is because of a naming convention. Generally, a class method that starts with new is expected to return a retained object and most other class methods are expected to return an autoreleased object. Since your method does not start with new but you are returning a retained object, the analyzer thinks that you are going to leak an object. The solution here is to give your method a better name, something like:

    + (MyDetails *) newMyDetails;
    

    There’s a few other conventions that you’re not following (and you probably should).

    1. Any method that begins with init is an initializer. It should be an instance method and it should return either self or the results of another initializer. Read Apple’s documentation on how to properly create an init method.
    2. Class names always start with capitalized letters. Your class should be named MyDetails.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does the following line leak this object [[NSMutableDictionary alloc] init] ? [myCollection setObject:[[NSMutableDictionary alloc]
//creates memory leak self.editMyObject = [[MyObject alloc] init]; //does not create memory leak MyObject
I have a memory leak when i call a method that return me a
Why does this leak? The arrayOfPerformances is a NSMutableArray , (nonatomic, retain) property that
Whilst investigating a memory leak I discovered that it was caused by calling NewRow()
I'm getting the memory leak message upon shutdown, saying that I'm leaking 3 of
Does this line leak if we remove TextBox from the visual and logical tree?
Does this leak?: static PyObject* foo(PyObject* self, PyObject* args){ PyObject* list = PyList_New(0); for(int
I've got a memory leak in Instruments... I've sorted some before, but this one
I have just resolved a memory leak in my application and now I want

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.