Note that there are some explanatory texts on larger screens.

plurals
  1. POProper, memory-managed implementation of custom NSDictionary getter
    primarykey
    data
    text
    <p>Thanks in advance...</p> <p>So after some recent memory leaks and bugs (documented <a href="https://stackoverflow.com/questions/7623989/plugging-a-sortedarrayusingselector-and-or-initwitharray-memory-leak-between-clas" title="here">here</a>), 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.</p> <p>The reasons for the custom getter are:</p> <p>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).</p> <p>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.</p> <p>So, the questions. The code below represents my first ever (studied) attempt at writing custom setter/getter, I usually just synthesize.</p> <p>1) Is this proper implementation and proper memory management?</p> <p>2) Is this best?</p> <p>3) Is the BOOL <code>installedStandardsChangedSinceLastRead</code> 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 <code>_installedStandards</code> instead, since that would also trigger the recalculate?</p> <p>4) Finally, if I <strong>did</strong> 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. <code>self.installedStandards = nil</code>), nil the iVar directly (i.e. <code>_installedStandards = nil</code>), or something else?</p> <p>(Oh, and if I nil'd the iVar directly, would I need to release it first? It seems like correct would be <code>[_installedStandards release]; _installedStandards = nil</code>? In which case couldn't I just use <code>self.installedStandards = nil</code>?)</p> <p><strong>Onto the code!</strong> <em>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.</em></p> <p><strong>.h file</strong></p> <pre><code>@interface InstalledStandardTracker20 : NSObject { NSDictionary *_installedStandards; BOOL _installedStandardsChangedSinceLastRead; } @property (nonatomic, retain) NSDictionary *installedStandards; @property (nonatomic) BOOL installedStandardsChangedSinceLastRead; @end </code></pre> <p><strong>@implementation</strong></p> <pre><code>@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; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload