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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:52:39+00:00 2026-05-16T18:52:39+00:00

I have this userInputstring in the header that will be modified and used by

  • 0

I have this userInputstring in the header that will be modified and used by multiple methods in the .m file

.h
 NSString *userInputString;
    -(void)main;
    -(void)method1;
    -(void)method2;

.m
    -(void)main{
    [self method1];
    [self method2];

    }


    -(void)method1{
    NSString *localString = @"something";
    userInputString = localString;
    //do something else with it
    }


    -(void)method2{
    NSString *localString = [NSString stringWithFormat:@"%@ insert something",userInputString];
    userInputString = localString;
    [someOtherMethod:userInputString];//Crash
    }

but I kept getting memory leak problems. What’s the proper way to set it up? Im new to objective c.

  • 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-16T18:52:40+00:00Added an answer on May 16, 2026 at 6:52 pm

    I don’t know where or how to release

    Right, you first need to familiarise yourself with the Cocoa Memory Management Rules.

    In summary, if you obtain an object by alloc, a method containing “copy”, a method starting with “new” or if you retain it, you need to release or autorelease.

    Take method1:

    -(void)method1{
        userInputString = @"something";
        }
    

    userInputString was not obtained with alloc, new or copy, nor have you retained it. Therefore you do not own it so you must not release it. If you had done this:

    userInputString = [@"foo" copy];
    

    or this:

    userInputString = [[NSString alloc] initWithString: @"foo"];
    

    or this:

    userInputString = [@"foo" retain];
    

    you do own the string therefore you must release or autorelease it.

    When you release it depends on its scope. If it’s a local variable, you must release or autorelease it before the block it is declared in exits. If it is an instance variable, you must release it before the object it is in is deallocated. i.e. you must release it in the dealloc method for the object. In all cases, if you overwrite an object you own, you must release it first. So:

    userInputString = [someOtherString copy];  // you own userInputString
    
    // do some stuff
    
    [userInputString release];                 // you no longer own it
    userInputString = [someOtherString retain];// overwrite the pointeer with something else
    

    This is one of the reasons for adding getters and setters for instance variables. Every time you set a new value, you have to release the old value and retain the new value (making sure that the old bvalue and new value are different), so this is encapsulated in the setter. A synthesized property adds this code automatically.

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

Sidebar

Related Questions

have this code: void set(list<Person*>* listP){ Person timmy = Person(10); listP->push_back(&timmy); } int main()
I have this method: -(NSString *)scrambleWordGenerator: (NSUInteger)length { NSMutableString *scrambledWord = [[NSMutableString alloc] initWithString:@];
I have this code that I have written in Ruby, but when trying to
I have this xml response to which I will do a foreach($response->entry->item as $data)
I have this line in my htaccess file: RewriteRule (.*) index.php This catches all
have this code in a htaccess file to hide the final php extension of
I have this subroutine that is passed in a chunk of xml and tries
I have this batch file to export two files from svn. But it never
I have this code in jQuery, that I want to reimplement with the prototype
I have this RewriteRule that works too well :-) RewriteRule ^([^/]*)/$ /script.html?id=$1 [L] The

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.