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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T19:43:04+00:00 2026-06-06T19:43:04+00:00

i m little confused on memory management in following scenario -(NSData *)getData{ NSData *TempData

  • 0

i m little confused on memory management in following scenario

-(NSData *)getData{ 
    NSData *TempData = [[NSData alloc]init];
    return TempData; 
  }

 //in some other method
 NSData *NewData = [self getData];

now the question is till when NewData will be retained and how should i release it?

  • 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-06T19:43:07+00:00Added an answer on June 6, 2026 at 7:43 pm

    This is wrong usage of this getter method.
    It should return an autoreleased copy of NSData. The autoreleased copy may or maynot be retained by the caller. Incase it is retained, its the caller’s responsibility to release it.

    Secondly a typo in return of this method, it should be NSData*.
    My typo in return. Thanks to Peter Sarnowski for pointing.

    -(NSData *)getData { 
        NSData *tempData = [NSData data];
        return tempData; 
    }
    

    The caller can retain it by using the following code:

    NSData *myData = [[self getData] retain];
    //Some code
    
    [myData release];
    

    In case caller is not interested in retaining this object, he will just call

    NSData *myData = [self getData];
    

    and use within the same function from where it is called.

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

Sidebar

Related Questions

I'm a little bit confused on how handle memory management when dealing with graphics
So I'm a little confused about Qt's memory management. I was looking at the
i'm a little bit confused with memory management in view controllers. Lets say i
Still a little confused about Objective-C memory management. I think my confusion stems from
I'm working on a memory pool implementation and I'm a little confused about pointers
I am a little confused on how exactly postScale method calculates the values of
I am little confused about vptr and representation of objects in the memory, and
I am a little confused about memory clean up in an ASP.NET application. I
I'm a little confused about how ValueType objects are managed memory-wise in .NET. I
I am a little confused on HttpServlet usage of @Path(/path) with an @POST method

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.