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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:21:46+00:00 2026-06-17T23:21:46+00:00

I have a class that manages the database queries for my project and I’m

  • 0

I have a class that manages the database queries for my project and I’m having some issues with memory management and the [return autorelease]part of the code. I don’t know how to return an object to the calling function but it uses it afterwards.
The code is this:

-(Hexagram *)getHexagramforId:(NSInteger)hexagramNumber{
    Hexagram *current;
    if (init_statement == nil) {
        const char * sql = "SELECT * FROM HEXNUMBER where _id =?";
    if (sqlite3_prepare_v2(database, sql, -1, &init_statement, NULL) != SQLITE_OK) {
        NSAssert1(0, @"Error:failed to prepare argument with message %s", sqlite3_errmsg(database));
    }
    sqlite3_bind_int(init_statement, 1, hexagramNumber);
    if (sqlite3_step(init_statement) == SQLITE_ROW) {
        current = [[Hexagram alloc]initWithId:hexagramNumber
                                             Title:[NSString stringWithUTF8String:(char*)sqlite3_column_text(init_statement, HEX_TITLE)]
                                               LongText:[NSString stringWithUTF8String:(char*)sqlite3_column_text(init_statement, HEX_TEXT)]ShortText:nil InternalColor:sqlite3_column_int(init_statement, INTERNAL_COLOR) ExternalColor:sqlite3_column_int(init_statement, EXTERNAL_COLOR)];

    }else{
        NSAssert1(0, @"Database is corrupt, error %s", sqlite3_errmsg(database));
    }
    sqlite3_reset(init_statement);
    sqlite3_close(database);
    init_statement = nil;
    return current;
}
return nil;

}

So what happens is in the analyzer it says that the object current hexagram is leaked, but if I return it with an autorelease it crashes. I want to support iOS < 5 so I don’t want to use ARC but so far I don’t know how to return this object and not get this error in the static analyzer. Any help please?

  • 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-17T23:21:47+00:00Added an answer on June 17, 2026 at 11:21 pm

    First off, the reason the analyzer is complaining is because you’re creating your Hexagram object via alloc] init, and returning it like that, which means your object will start out with a retain count of 1. Unless your method names starts with new/alloc/copy, the user of the code (and static analyzer) will be expecting an autoreleased object. (See documentation of naming conventions + memory management).

    As for autorelease; Autorelease will give an object a retain count of 1 (so that it can hang around for a bit), but will be released at the end of a run loop. This will cause a retain count of 0, and the object gets deallocated.

    Now, why does return [current autorelease] fail? The reason will be because of the caller of the method getHexagramforId. If someone is calling a method that returns an autoreleased object (a method that does not start with new/alloc/copy etc…), then it is their responsibility to keep hold of that object when it needs to (for the reasons mentioned above).

    So for example, if you changed return current to return [current autorelease], and called the method as so:

    Hexagram* obj = [[SomeClass getHexagramforId:3] retain];
    

    your program shouldn’t crash. You are explicitly retaining an autoreleased object, so you now can keep that object around, until you no longer need it. At that point, call [obj release].

    Other methods for retaining an autoreleased object include assigning it to a retaining property, as so:

    @property (nonatomic, retain) Hexagram* hexObj;
    
    self.hexObj = [SomeClass getHexagramForId:3];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a DBAdmin class that connects to the database, and then some other
I have an interface IRenderable and a class that manages rendering all instances of
Im making a program for class that manages a Hotel. I have a function
I have a GUI tool that manages state sequences. One component is a class
I have a singleton class which manages data and database handling of my application.
I am trying to test a class that manages data access in the database
I have a PHP class called Vehicle that manages everything pertaining to cars,motorbikes and
I have created a database and a bundle that manages users, guests, categories: A
I have created a class that will manage connection and commands, I named it
In my game I have a concept of resource manager, a class that stores

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.