Goal c - Free a Singleton Property

I have a singleton class that has mutableDictionary. I initialize the dictionary in my root viewController. later I would like to free the dictionary and free the memory. Although the hold count is 1, the release causes a failure:

- [CFDictionary release]: message sent to the freed instance

Is it possible to release the singleton property?

thank

+3
source share
4 answers

First I repeat what has been said many times here: Do not call -retainCount!! This is an implementation detail.

See: StackOverflow | when to use saveCount for an excellent recount on why you are not using saveCount.

, . , ( , ) . . , .

, :

, http://bugreport.apple.com , -retainCount . , .

+4

. singleton , .

, Cocoa , , " 1", , - , :

!

, , .

, , . , 0 - 1, . , , - , 0 - 0 .

+1

. , . , , . singleton, singleton , .

0

re retainCount. .

  • /

  • , ,

  • [dictionary removeAllObjects], ,

  • If the dictionary stores the objects that you want to unlock in the event of a memory warning, perform a one-time observation UIApplicationDidReceiveMemoryWarningNotificationand delete all its objects there.

  • If you really want your implementation to free the entire dictionary, I would override the synthesized getter and add singleton methods to interact with the dictionary as follows:

in MySingleton.m:

- (NSMutableDictionary *)myDictionary
{
    if (!_myDictionary) {
        _myDictionary = [[NSMutableDictionary alloc] init];
    }
    return _myDictionary;
}

- (void)setObject:(id)object inMyDictionaryForKey:(NSString *)key
{
    [self.myDictionary setObject:object forKey:key];
}

- (void)removeObjectInMyDictionaryForKey:(NSString *)key
{
    [self.myDictionary removeObjectForKey:key];
    if ([self.myDictionary count] == 0) {
        self.myDictionary = nil;
    }
}

- (void)removeAllObjectsFromMyDictionary
{
    self.myDictionary = nil;
}
0
source

Source: https://habr.com/ru/post/1795814/


All Articles