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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:26:52+00:00 2026-05-23T11:26:52+00:00

I am trying to understand why my application crashes and I am going through

  • 0

I am trying to understand why my application crashes and I am going through my code. I am pretty sure that this is a valid use of autorelease:

(part of the code)

- (NSArray *)allQuestionsFromCategories:(NSArray *)categories {

    ...

    NSMutableArray *ids = [[[NSMutableArray alloc] init] autorelease];

    while (sqlite3_step(statement) == SQLITE_ROW) {
        [ids addObject:[NSNumber numberWithInt:sqlite3_column_int(statement, 0)]];
    }

    return [NSArray arrayWithArray:ids];
}

Is this valid? The NSArray arrayWithArray returns an autorelease object doesn’t it? I also have some difficulties in understanding the scope of autoreleased objects. Would the autoreleased object (if it is in this case) be retained by the pool through out the method that invoked the method that this code is a part of?

- (void)codeThatInvokesTheCodeAbove {
    NSArray *array = [self.dao allQuestionsFromCategories];
    ...
}

Would the array returned be valid in the whole codeThatInvokesTheCodeAbove method without retaining it? And if it was, would it be valid even longer?

Got some issues understanding the scope of it, and when I should retain an autorelease object.

  • 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-23T11:26:52+00:00Added an answer on May 23, 2026 at 11:26 am

    [NSArray arrayWithArray:] returns an autoreleased object. If you want codeThatInvokesTheCodeAbove to take ownership of the array, you should call retain on it (and rename codeThatInvokesTheCodeAbove according to apple’s guidelines). Otherwise, if you don’t care that ownership of the object is ambiguous then your code is okay.

    In other words, [NSArray arrayWithArray:] returns an array that you don’t own, but you have access to it for at least this run cycle. Therefore, codeThatInvokesTheCodeAbove will have access to it for at least this run cycle. Ownership is not clear, since nobody called alloc, copy, new, or mutableCopy or retain. It is implied that NSArray called autorelease before returning the new array, thus relinquishing ownership.

    My information comes from http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmRules.html%23//apple_ref/doc/uid/20000994-BAJHFBGH.

    So, to answer your question, yes your posted code is valid. Whether it’s correct depends on what it is you are trying to accomplish.

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

Sidebar

Related Questions

I am trying to understand my embedded Linux application's memory use. The /proc/pid/maps utility/file
Trying to understand Ruby a bit better, I ran into this code surfing the
I'm trying to understand how the Application class. I've noticed that need to declare
Trying to understand the Android framework model. I have an application that needs to
I am implementing web services for a PHP application and am trying to understand
I'm trying to include the Sparkle framework in my application. I don't really understand
After trying to understand why client code is not rendered in a page (injected
Trying to understand, why my C++/Qt application creates 18 threads, while i don't create
I'm trying to understand an old classic Mac application's entry point. I've disassembled the
I'm trying to understand a Spring 3.0 application which contains the following BeanPostProcessor implementation.

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.