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

  • Home
  • SEARCH
  • 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 7568773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:00:29+00:00 2026-05-30T15:00:29+00:00

I had an instance in one of my classes where I neglected to properly

  • 0

I had an instance in one of my classes where I neglected to properly retain a passed in parameter. It was quite time consuming to track down.

In fixing this I want to create a test for that situation but I’m unsure how to proceed. It seems not as easy as creating the object and testing each member as this can pass the test depending on whether the autorelease pool decided to let go of the object right away or not.

My question is: Is there a way I can create the object and assure that any autorelease happens before I do my tests? Should I create my own pool in the test case and release before I preform that ivar testing? Will that catch this case 100% of the time? Should I wrap ALL my tests in an autorelease pool to proactively catch any potential crashes?

  • 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-30T15:00:31+00:00Added an answer on May 30, 2026 at 3:00 pm

    If you use a constructor that gives you a non-autoreleased object, then you don’t have to worry about this. But otherwise, you do need to use autorelease pools.

    This will get you an EXC_BAD_ACCESS immediately if and only if array is a not retained by the setter, since its retain count goes to 0 on the release and it gets immediately deallocated:

    NSArray *anArray = [[NSArray alloc] init];
    self.array = anArray;
    [anArray release];
    NSLog(@"Here's my array: %@", self.array); // EXC_BAD_ACCESS if array not retained
    

    Whereas this wouldn’t cause any errors, even though self.array would become garbage later on, as soon as the autorelease pool was drained:

    self.array = [NSArray array];
    NSLog(@"Here's my array: %@", self.array); // No error for now
    

    If you have to use a convenience constructor, just wrap the assignment in an autorelease pool:

    NSAutoreleasePool *testPool = [[NSAutoreleasePool alloc] init];
    self.array = [NSArray array];
    [testPool drain];
    NSLog(@"Here's my array: %@", self.array); // EXC_BAD_ACCESS if array not retained
    

    This way you’ll get an EXC_BAD_ACCESS again as long as array isn’t retained by the setter.

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

Sidebar

Related Questions

Had a good read through similar topics but I can't quite a) find one
I had a need of a quick unique ID in one of my classes
I've never actually had an instance where this was required, but here I am...
Had this working; at one stage. The problem is the following text is now
In a project I'm creating, I have various classes. One of my classes has
If you had classes like Page, News, etc. that describe content items and you
I'm just learning Java... I have 2 custom classes. One is a Fraction and
I had some discussion related to the use of properties and instance variables at
So, one of my classes has the following overload: DVector &operator+=(DVector const &other) {
Imagine in the Global.asax.cs file I had an instance class as a private field.

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.