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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T09:17:15+00:00 2026-06-10T09:17:15+00:00

I am getting a static analysis error in this code which doesn’t make any

  • 0

I am getting a static analysis error in this code which doesn’t make any sense to me. The error is:

Reference-counted object is used after it is released

This is glue code to allow for PNG loading in a game originally written in C++.

int pngLoad(const char *filename, pngInfo *info, int format, GLuint *textureName)
{
    char fullPath[Engine::Settings::MaxPath];
    strcpy(fullPath, filename);
    appendAppBundlePath(fullPath);

    NSString *path = [NSString stringWithCString:fullPath encoding:NSUTF8StringEncoding];
    NSData *data = [[NSData alloc] initWithContentsOfFile:path];
    UIImage *image = [[UIImage alloc] initWithData:data];
    [data release];

    Texture2D *loadedTex = [Texture2D alloc];

    // ##### Analyzer claims the object is released here: #####
    [loadedTex initWithImage:image format:format];

    int didLoad;

    // ##### Error is here: #####
    if (loadedTex.contentSize.width == 0 || loadedTex.contentSize.height == 0)
    {
        didLoad = 0;
    }
    else
    {
        didLoad = 1;

        *textureName = loadedTex.name;

        // return texture info
        info->ScaleFactor = loadedTex.scaleFactor;
        info->Width = (float)image.size.width / (float)info->ScaleFactor;
        info->Height = (float)image.size.height / (float)info->ScaleFactor;
        info->Alpha = 1;
        info->PaddedWidth = loadedTex.pixelsWide;
        info->PaddedHeight = loadedTex.pixelsHigh;
    }

    [loadedTex release];
    [image release];

    return didLoad;
}

If I use Texture2D *loadedTex = [[Texture2D alloc] retain]; this warning is removed, but then an warning that I’ve leaked an object comes up, so something is seriously weird here.

initWithImage:format: used to contain a [self release] which shouldn’t have been there, which I removed when I found this warning. However, even after a full clean and rebuild, I still get the warning. Am I doing something else wrong? Is something not getting properly cleaned up by the Clean command in Xcode?

  • 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-10T09:17:16+00:00Added an answer on June 10, 2026 at 9:17 am

    The analyzer may be right, at least in a general way.

    Texture2D *loadedTex = [Texture2D alloc];
    [loadedTex initWithImage:image format:format];
    

    In general, “init” might actually discard the object passed in and return a different one. Whether or not this is the case for “Texture2D” is something I don’t know, but if the analyzer is going for the general case then it is right.

    You should be able to work around that by using

    Texture2D *loadedTex = [Texture2D alloc];
    loadedTex=[loadedTex initWithImage:image format:format];
    

    Or by simply combining the two calls, as it is done in most Objective-C examples.

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

Sidebar

Related Questions

I'm getting this code analysis error: CA1823:AvoidUnusedPrivateFields, for this line of code: private static
I am getting this error: An object reference is required for the non-static field,
I am getting an error An object reference is required for the non-static field,
I am getting code analysis error in the below method. public static OracleCommand CreateStoredProcedureCommand(string
I'm getting a potentially leaked object error from the Static Analyzer for this line:
I keep getting the following error in Eclipse: Type Cannot make a static reference
I'm getting An object reference is required for the non-static field, method, or property
I am getting ERROR when I am running this program at the line static
I'm getting a really strange NPE exception. This is the offending code: private static
I am running static code analysis with FxCop 1.36 and I keep getting warning

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.