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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T02:27:29+00:00 2026-05-19T02:27:29+00:00

I have a UITextField and in the delegate class I have a UITableView. Here

  • 0

I have a UITextField and in the delegate class I have a UITableView. Here is the code:

- (BOOL) textField: (UITextField *)theTextField shouldChangeCharactersInRange: (NSRange)range replacementString: (NSString *)string {    
    value = [[theTextField.text stringByReplacingCharactersInRange:range withString:string] retain];

    [valueTable reloadData];

    return YES;
}

“value” is an NSString declared at the top of my class as “NSString *value;” and “valueTable” is just a UITableView. When I test for memory leaks, I am getting a “100%” memory leak on the line “value = [[theTextField.text stringByReplacing…” and I tried removing the “retain” on that line. However then later on when I called upon “value,” it was nil, which is not good.

So how can I fix the memory leak? And what is the memory leak? Thanks!

  • 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-19T02:27:30+00:00Added an answer on May 19, 2026 at 2:27 am

    The memory that is being leaked is the memory pointed to by value.

    Every time your text field changes, the method stringByReplacingCharactersInRange... is returning an autoreleased NSString object. You are correct to retain it, so that it isn’t deallocated. The problem, is that you now own memory somewhere. (You own that NSString by retaining it.)

    The next time that method is called, when the user changes the text in that field, you’re pointing value to a completely different memory location. The original memory that you had retained still exists, and will continue to persist forever. (Since you never released it.)

    It’s very important to match any retain method calls with an associated release. You could do either:

    ...
    if (value) {
       [value release];
    }
    value = ...;
    ...
    

    OR

    You can define the NSString *value as a property on your class, like:

    @property (nonatomic, retain) NSString *value);
    
    /* Implementation file */
    @synthesize value;
    

    Then simply use:

    ...
    self.value = ...;
    ...
    

    Also, since you’re always going to have retained memory after that method has been called, you’ll need to release value when your class is deallocated, as was mentioned in another answer:

    - (void)dealloc {
        // Only do *one* of the two following releases
    
        // (1) If you're not using properties:
        [value release];
    
        // (2) If you are using properties:
        self.value = nil;
    
        [super dealloc];
    }
    

    Edit: It sounds like you should definitely read Apple’s memory management guide before continuing:
    http://developer.apple.com/library/mac/#documentation/cocoa/conceptual/MemoryMgmt/MemoryMgmt.html

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

Sidebar

Related Questions

Let's say I have the following code: IBOutlet UITextField* nameTextField; IBOutlet UILabel* greetingLabel; I'd
I have an UITextField in a MyCustomUIView class and when the UITextField loses focus,
How do I make a delegate? I have a class called CustomSign. The class
I have some questions about using delegate patten on iPhone. This is code using
I have this code to prompt the UIAlertView , with UITextfield : UIAlertView *myAlertView
In my application I have a UITextField inside a UITableViewCell . If I click
Have you guys had any experiences (positive or negative) by placing your source code/solution
I have a class that needs to ask the user a question and wait
I have a class inherited from UITableViewController and this is also the root class.
Is this possible? For example, I have a class TriangleDataView with an init 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.