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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T22:52:17+00:00 2026-05-31T22:52:17+00:00

First of all: I understand, why this warning is existing, I just can’t explain

  • 0

First of all:
I understand, why this warning is existing, I just can’t explain myself, why it is triggered under these circumstances.
The official Cocoa memory management policy is as following:
“You own any object you create
You create an object using a method whose name begins with “alloc”, “new”, “copy”, or “mutableCopy” (for example, alloc, newObject, or mutableCopy).

You can take ownership of an object using retain”

I know have code like this:

- (id) foo
{
    // do something
    return self;
}

- (id) init
{
    self = [super init];
    return [self foo]; // clang static analyzer is complaining about returning an object with a +0 retain count here, although a +1 would be expected
}

Afaik this is a false positive from clang, isn’t it?
I mean, the only methods, which increase the retain-count, are “retain” and all methods, whose names begin with “alloc”, “new”, “copy”, or “mutableCopy”, so “init” is not increasing the retain-count, but relays on the caller, to pass in the return-value of the class’ “alloc”-method, so actually “init” is expected to return a +0 retain count, not a +1 one, isn’t it? It just returns the object with the same retain count, with which it has been passed in.
Now calling “foo” on that object also does not change the retain count, so this code should be perfectly legal and in fact it works just fine and the retain count is correct all over the whole life time of the program, only the static analyzer is complaining.

  • 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-31T22:52:18+00:00Added an answer on May 31, 2026 at 10:52 pm

    The method name foo by convention means that it returns an object that the caller does not own.

    alloc–init*** combinations return an object that the caller owns.

    Looks like the analyzer can’t or doesn’t bother to look inside foo to see what it’s actually doing, and does not know that the returned value is the same object as the already owned self.

    It is not a false positive. If a subclass, or a dynamically loaded category override foo to return something else:

    - (id) foo; {
        return [NSNumber numberWithInt:666];
    }
    

    then you will have a leak of self and an over-release of NSNumber object.

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

Sidebar

Related Questions

First, of all, I already search about this but i still can't I understand
First of all, I hope you understand my English, because I'am not a native
first of all I apologize if my question is difficult to understand. I'm having
First of all, I do understand, that MVC 3 default template's Content folder is
All - I'm trying to understand the first section of the Map file produced
First of all, this isn't for a keylogger, it's for an input in a
First of all this is the first time I am asking a java question
First of all, this is not homework, so please don't tag it as homewrok
First of all I did not write this code. I found it on somebody
Background / Disclaimer First of all, please feel free to skip this entirely if

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.