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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T12:47:13+00:00 2026-05-15T12:47:13+00:00

I am getting leak at below code: search.h NSString *str; search.m -(void)searchMethod:(UISearchBar *)aSearchBar {

  • 0

I am getting leak at below code:

search.h

NSString *str;

search.m

-(void)searchMethod:(UISearchBar *)aSearchBar
{
     /*******Showing leak at below line *************/
     str=[aSearchBar.text stringByTrimmingCharactersInSet:
                   [NSCharacterSet whitespaceAndNewlineCharacterSet]];
 }

I am not getting how to solve this can any one help me to solve this.

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-15T12:47:14+00:00Added an answer on May 15, 2026 at 12:47 pm

    How do you know this is a leak? str is autorelease in your example, that means it will release itself some time after this method returned. Only if you would perform this task on a separate thread with no autorelease pool in place or with an auto release pool you never clear, str could leak. If this code runs on main thread, str will for sure not leak.

    I rather think you get a crash because you don’t retain str. You store str into a global variable, but without increasing the retain count. That means once str is autoreleased, your grobal variable points to invalid memory. To make sure an object stays alive beyond the scope of a method, you must retain it (unless you created this object via alloc/init…, new… or copy…).

    str = [aSearchBar.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    str = [str retain]; // Keep object alive beyond the scope of this method
    

    Of course if you keep an object alive like this, you must release it yourself somewhere in your code. So if you want this variable to be overridden each time the method is called, use

    [str release];
    str = [aSearchBar.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    str = [str retain]; // Keep object alive beyond the scope of this method
    

    Don’t forget to also release str within the dealloc method of this object (override dealloc for that; don’t forget to call [super dealloc] within the overridden method as last instruction).

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

Sidebar

Related Questions

I am getting NSCFString for the code below: NSString *urlString; /* leak showing below
I am getting the leak at this line in below code NSData *returnData =
i wrote the code like below and i am getting the memory leak at
I'm getting a leak from using substringWithRange as shown in the line of code
I am getting memory leak in instruments in the code -(void)connectionDidFinishLoading:(NSURLConnection *)connection { NSAutoreleasePool
Hi I am getting memory leak in Instruments for the following line of code
below is my code, Leaks says I am getting a memory leak around NSMutableString
Im getting the same memory leak as I mentioned the link below. NSXMLParser Leaking
I am getting leak at [pool release]; My code here is: #pragma mark UISearchBarDelegate
I am getting the following line as a leak in Xcode: someSprite = [[CCSprite

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.