Guest User

Untitled

a guest
Jul 18th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. - (void)testMemory {
  2. __weak id testingPointer = nil;
  3. id someObject = // some object with a 'foo' property
  4.  
  5. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  6. {
  7. // Point the weak pointer to the thing we expect to be dealloc'd
  8. // when we're done.
  9. id theFoo = [someObject theFoo];
  10. testingPointer = theFoo;
  11.  
  12. [someObject setTheFoo:somethingElse];
  13.  
  14. // At this point, we still have a reference to 'theFoo',
  15. // so 'testingPointer' is still valid. We need to nil it out.
  16. STAssertNotNil(testingPointer, @"This will never happen, since we're still holding it.")
  17.  
  18. theFoo = nil;
  19. }
  20. [pool drain];
  21.  
  22.  
  23.  
  24. // Now the last strong reference to 'theFoo' should be gone, so 'testingPointer' will revert to nil
  25. STAssertNil(testingPointer, @"Something didn't release %@ when it should have", testingPointer);
  26. }
Add Comment
Please, Sign In to add comment