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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:30:59+00:00 2026-05-26T05:30:59+00:00

Thanks in advance… So after some recent memory leaks and bugs (documented here ),

  • 0

Thanks in advance…

So after some recent memory leaks and bugs (documented here), I’ve been studying up on my memory management, and I am now trying to write a custom getter for an NSDictionary in a helper class I have.

The reasons for the custom getter are:

First, it’s a calculated dictionary, so for performance issues I want to return the cached object unless it’s necessary to regenerate it (my example below isn’t too computationally expensive, but others in the class will be).

Second, I want to lazily instantiate. I only want to generate the dictionary if another class “asks” for it, which is why I’m checking for nil in the getter.

So, the questions. The code below represents my first ever (studied) attempt at writing custom setter/getter, I usually just synthesize.

1) Is this proper implementation and proper memory management?

2) Is this best?

3) Is the BOOL installedStandardsChangedSinceLastRead the best way to flag the need to recalculate? This would happen if other methods in the class had changed something. But should I just have those methods nil out _installedStandards instead, since that would also trigger the recalculate?

4) Finally, if I did just have the other methods nil out the iVar to flag it for recalculation, how would I make sure I don’t leak? Would I use the setter (i.e. self.installedStandards = nil), nil the iVar directly (i.e. _installedStandards = nil), or something else?

(Oh, and if I nil’d the iVar directly, would I need to release it first? It seems like correct would be [_installedStandards release]; _installedStandards = nil? In which case couldn’t I just use self.installedStandards = nil?)

Onto the code!
Reminder that this is a simplified version of what will be a more complex class (see link at top) with lots of these setters/getters. I need to make sure I’m executing properly before I flesh it out.

.h file

@interface InstalledStandardTracker20 : NSObject {

    NSDictionary *_installedStandards;
    BOOL _installedStandardsChangedSinceLastRead;
}

@property (nonatomic, retain) NSDictionary *installedStandards;
@property (nonatomic) BOOL installedStandardsChangedSinceLastRead;

@end

@implementation

@implementation InstalledStandardTracker20

@synthesize installedStandardsChangedSinceLastRead = _installedStandardsChangedSinceLastRead;

- (void)refreshInstalledStandards {
    NSUserDefaults *currentDefaults = [NSUserDefaults standardUserDefaults];
    self.installedStandards = [currentDefaults objectForKey:@"installedStandards"];
    self.installedStandardsChangedSinceLastRead = NO;
}

- (NSDictionary *)installedStandards {
    if (!_installedStandards || self.installedStandardsChangedSinceLastRead) {
        [self refreshInstalledStandards];
    }
    return _installedStandards;
}

- (void)setInstalledStandards:(NSDictionary *)newInstalledStandards {
    [newInstalledStandards retain];
    [_installedStandards release];
    _installedStandards = newInstalledStandards;
}
  • 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-26T05:30:59+00:00Added an answer on May 26, 2026 at 5:30 am

    This question is rather difficult to answer because there is no context outside of the class. The code appears to be memory-leak free. Also, you don’t need to implement -setInstalledStandards:. Since you call @synthesize on a retain property, the generated setter would look just like that. In essence, you’re overriding the method with exactly the same method.

    For your other question, nilling out an iVar has nothing to do with what objects are being held in memory. As soon as you release the iVar, the object at the memory address contained in your pointer is no longer guaranteed to exist. By nilling the iVar, you are simply resetting that memory address to point to nil. This avoids you getting an EXC_BAD_ACCESS runtime exception because no other parts of your program will attempt to access this now undefined address space.

    In essence, you always have to release it. While nilling it is good practice, it is not required so long as you don’t try to access that pointer until you know it contains a valid object in memory. I hope that makes sense.

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

Sidebar

Related Questions

First of all thanks in advance, this has been very frustrating and I'm hoping
Hello and thanks in advance for the communal help I always find here. I
Thanks in advance, I feel like I'm overlooking something very simple. I have some
Hi thanks in advance... I am Working on a Project, I need some clarification
Thanks in advance to anyone who can help. I've been searching for an answer,
Thanks in advance for any help. :) Ok, here's my problem. Simplified version of
thanks in advance for your help. I am wondering if there is a (design)
Thanks in advance for your help experts. I want to be able to copy
Thanks in Advance for reading and answer this question. I got button in asp
Special thanks in advance for sending me answer........ I m the beginner in iphone

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.