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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T04:48:49+00:00 2026-06-08T04:48:49+00:00

In the following code xCode’s Build & Analyze function detects a Potential leak of

  • 0

In the following code xCode’s Build & Analyze function detects a

Potential leak of an object allocated on line 165 and stored into ‘addButton’.

addButton is a UIBarButtonItem using the category barItemWithImage (which I read about here) which returns an autoreleased object. If I don’t retain addButtonItem I get an exception on trying to access a released object.

What am I missing here?

        UIBarButtonItem *addButton;

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
        addButton = [UIBarButtonItem barItemWithImage:[UIImage imageNamed:@"RedPlus.png"] target:self action:@selector(createStoryModal:)];
    }else {
        addButton = [UIBarButtonItem barItemWithImage:[UIImage imageNamed:@"RedPlusiPhone.png"] target:self action:@selector(createStoryModal:)];
    }

    [addButton retain];

    NSArray* toolbarItems = [NSArray arrayWithObjects:
                             addButton,
                             [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace 
                                                                           target:nil
                                                                           action:nil],
                             nil];

    [toolbarItems makeObjectsPerformSelector:@selector(release)];
    self.toolbarItems = toolbarItems;

The category code:

    @implementation UIBarButtonItem(MyCategory)

+ (UIBarButtonItem*)barItemWithImage:(UIImage*)image target:(id)target action:(SEL)action{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:image forState:UIControlStateNormal];
    [button setFrame:CGRectMake(0.0, 0.0, image.size.width, image.size.height)];
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];

    return [[[UIBarButtonItem alloc] initWithCustomView:button] autorelease];
}

@end

  • 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-08T04:48:52+00:00Added an answer on June 8, 2026 at 4:48 am

    the static analyzer is right.

    remove this:

    [addButton retain];
    

    and this:

    [toolbarItems makeObjectsPerformSelector:@selector(release)];
    

    and you’re also leaking a UIBarButtonItem.

    NSArray retains its elements.

    There may be other memory issues elsewhere, but that should remove three of the visible issues/bad forms.

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

Sidebar

Related Questions

I'm using the following code within XCode, building for iOS with ARC enabled. Why
I have the following warning in my code (XCode 4.3 / iOS 4/5) -
Following code: <%= render 'shared/error_messages', f.object %> where f.object is instance of a class
I'm using the following code in xCode, to work out how much to initially
since I updated to xcode 4.2 and iOS 5 following code has no effect:
The following code generates a compile error in Xcode: template <typename T> struct Foo
I have a strange situation where the following code works however XCode warns it
I have the following code snippet in my Xcode: NSString *digit [[sender titlelabel] text];
I wrote the following code under MacOSX in XCode. When moving the code over
I just created an Xcode project and wrote the following code: #define foo(x) x

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.