A better solution is simply
- (void)testDealloc
{
__weak CLASS *weakReference;
@autoreleasepool {
CLASS *reference = [[CLASS alloc] init]; // or similar instance creator.
weakReference = reference;
// Test your magic here.
[...]
}
// At this point the everything is working fine, the weak reference must be nil.
XCTAssertNil(weakReference);
}
This works creating an instance to the class we want to deallocate inside @autorealase
, that will be released (if we are not leaking) as soon as we exit the block. weakReference
will hold the reference to the instance without retaining it, that will be set to nil
.
dealloc
directly (except[super dealloc]
indealloc
) when not using ARC. – Hindermost