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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:34:57+00:00 2026-05-25T06:34:57+00:00

i have function with return type as (NSArray/NSData/NSString/NSDictionary and so on). i can return

  • 0

i have function with return type as (NSArray/NSData/NSString/NSDictionary and so on). i can return and use as well ,but my problem arise @ release the object. Please guide me which one is best way to manage memory for returned object.if anything is wrong please ignore and give your best solution

i have refereed apple Memory management guide

A)

-(NSData *)somefunction2
{  
   NSData *data=[[[NSData alloc]init]autorelease];  
    // fill stuff for nsdata
   return data;
}

-(void)somefunction
{    
NSData *data=[self somefunction2];    
// use data    
}

but most of the blogs advice to avoid autorelease so i was using following type

B)

-(NSData *)somefunction2
{  
   NSData *data=[[NSData alloc]init];  
    // fill stuff for nsdata
   return data;
}

-(void)somefunction
{

NSData *data=[[self somefunction2] retain];

// use data

[data release];

}

C)

-(NSData *)somefunction2
{  
   NSData *data=[[[NSData alloc]init]autorelease];  
    // fill stuff for nsdata
   return data;
}

-(void)somefunction
{

NSData *data=[[self somefunction2] retain];

// use data

[data release];

}

Edit:
One more thing. if i try to pass Same allocted object to Some function call Argument or custom Delegate object ,Where i have to Release ????Whether below the function call or get retain in function def and then release.

NSData *data=[[NSData alloc]init];

[self somfunctioncall:data];

or

NSData *data=[[NSData alloc]init];

[delegate mydelegatefunction:data];


-(void)somfunctioncall:(NSdata *)data
{
NSData *newdata =[data retain];
//data use
[data release];}

Thanks in advance

  • 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-25T06:34:58+00:00Added an answer on May 25, 2026 at 6:34 am

    The best practice is that if the method contains either alloc, init, new or retain, the method returns a retained object, and if it doesn’t it is returning an autoreleased object. I would suggest this:

    -(NSString *)newData
    {  
       NSData *data=[[NSData alloc]init];  
        // fill stuff for nsdata
       return data;
    }
    

    So that later you can just do this:

    -(void)somefunction
    {
    
    NSData *data=[self newData];
    
    // use data
    
    [data release];
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Some C++ compilers allow the main function to have return type void . But
I have a function...in which I have return type as table variable... But performance
Can I have a function with zero arguments and no return type? If the
In python, you can have a function return multiple values. Here's a contrived example:
I have a function which return a type int. However, I only have a
I have this function in PostgreSQL, but I don't know how to return the
I have a function with a List return type. I'm using this in a
Is it possible to have a function or macro to return variable type? I
I have a template function that I wish to return either the type of
I have an OpenCV function which return type is Mat. How do I convert

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.