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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:16:50+00:00 2026-06-13T23:16:50+00:00

I dont want to return manch because if i autorelease before i return it

  • 0

I dont want to return manch because if i autorelease before i return it ,it becomes invalid to others. so i was thinking of this :

 classA
-(NSMutableArray*)set:(NSMutableArray*)data
    {
        manch=  [[data mutableCopy]autorelease]  ;
        int count=2*[data count]; //to not enter infinity loop

        for(int k=0;k< count;k=k+2)
        {
            if(k==count-1)
                [manch addObject:[NSNumber numberWithInt:![[manch objectAtIndex:k] integerValue] ] ];
        }

        data=[manch mutuableCopy];
        return data;
    }

My goal is to create a class that gets an NSMutuableArray do some calculations, than return it, and NOT TO BE DEPEND on this class anymore .

EDIT :

As people here ask.
in another classB(the user of the method above), i have in the interface :

NSMutuableArray *data ;

and on the .m file init method i have

data=[[NSMutuableArray alloc]init];

to use the function from my question, i do :

mIns=[[classA alloc]init];
data= [mIns set:[self decimalToBinary:autoWord]];

than i loose data later.

  • 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-13T23:16:51+00:00Added an answer on June 13, 2026 at 11:16 pm

    I dont want to return manch because if i autorelease before i return it ,it becomes invalid to others. so i was thinking of this:

    This is an incorrect statement, you can return an autoreleased object, that’s a sane thing to do. It’s worth noting that you should design your method names correctly to inform the user what sort of object is returned. Any method whose name begins with alloc, new, copy, or mutableCopy will return a retained object. (Source)

    In your case, your method name is set:, which informs the user of this method that it will return a non retained object (almost always an autoreleased object). This is because it isn’t prefixed with any of those words mentioned above.

    In that case, the issue you have is with the user of the method; they are not retaining a reference to the object being returned. As such, the user of the method should use it as so:

    @interface ClassName () {
        NSMutableArray* m_ivarArray;
    }
    @property (nonatomic, retain) NSMutableArray* propertyArray;
    @end
    
    NSMutableArray* data = ...;
    
    // If using a property with retain, setting via "self." will retain it for you
    self.propertyArray = [self set:data];
    
    // If using an ivar (which doesn't do the retain for you)
    m_ivarArray = [[self set:data] retain];
    

    You can avoid these issues by using Automatic Reference Counting (ARC, More Information), which will handle this sort of memory management for you. It is still important that you use the correct naming conventions, as ARC will judge how to manage your memory based on this (in certain situations)

    Update: After seeing your update, I can see the problem.

    data=[[NSMutuableArray alloc]init];
    

    This is creating a new instance of NSMutableArray, one which is correctly retained (due to what I mentioned before).

    data= [mIns set:[self decimalToBinary:autoWord]];
    

    This is replacing the object held in data with a new NSMutableArray, one that is autoreleased. The previous instance you created has been lost, and you’ve replaced it with another one. This new instance has not been retained, and as such, will be released unexpectedly.

    To fix, you need to use this instead:

    NSMutableArray* data = [[mIns set:[self decimalToBinary:autoWord]] retain];
    

    You don’t need to alloc/init a variable if it will be populated by some other object later on. I strongly suggest brushing up on how this all works, this might be a good start.

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

Sidebar

Related Questions

Is it possible to disable the OData support? I dont want this automatism because
I want to make a movieclip invisible initially but i dont want to set
If you dont want the long sschpeal head the the last paragraph--> I found
I want to show terms and condition note on my website. I dont want
I am making an application that I dont want it to close. So on
Although I am using mySQL (for now), I dont want any DB specific SQL.
I'm building a GWT Application. But i dont want user to enter any html
i need to log each method call in my application. i dont want to
May I install a search provider/accelerator remotely in Internet Explorer 8? I dont want
my footer wont go to the bottom below everything. i dont want it fixed

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.