cool hit counter How to tell when an object is freed in iosARC_Intefrankly

How to tell when an object is freed in iosARC


First let's understand the NSHashTable

NSHashTable follows the example of NSSet (NSMutableSet), but offers more manipulation options than NSSet, especially in terms of support for weak reference relations, and NSHashTable is more flexible when it comes to object/memory handling. Compared to NSSet, NSHashTable has the following features.

  • NSSet (NSMutableSet) holds strong references to its elements, and these elements are used with hash values and the isEqual: method to do hash detection and determine if they are equal.
  • NSHashTable is mutable, it has no immutable version.
  • It can hold a weak reference to an element and can correctly remove the object after it has been destroyed. And that can't be done at NSSet.
  • Its members can be copied as they are added.
  • Its members can use pointers to identify equality and do hash detection.
  • It can contain arbitrary pointers, and its members are not restricted to objects. We can configure an NSHashTable instance to manipulate arbitrary pointers, not just objects.
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    [self testWeakMemory];
}

Adding the object to be observed to the HashTable

- (void)testWeakMemory {
    if (!_hashTablele) {
        _hashTablele = [NSHashTable weakObjectsHashTable];
    }
    _obj = [[NSObject alloc] init];
    [_hashTablele addObject:_obj];
    NSLog(@"hashTablele: %@", _hashTablele);
}

The HashTable determines if the object exists, and if not it is freed

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.obj = nil;
    
    if ([_hashTablele containsObject:_obj]) {
        NSLog(@"obj The object was not released");
    }
    else{
        NSLog(@"obj  The object has been released");
    }
     NSLog(@"hashTablele: %@", _hashTablele);
}

Recommended>>
1、Dry ShareDeep Learning ZeroBased Progression Part 2
2、See the big AI tech breakthroughs throughout 2018 in one article
3、Technology Key Technologies for Big Data
4、Setting up jupyternotebook with remote access
5、Python Brush Janes Book Views Reads Happy Dragon Boat Festival Stay up late for the World Cup and brush up to 10k first Simple crawler commonpy

    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号