I had an instance in one of my classes where I neglected to properly retain a passed in parameter. It was quite time consuming to track down.
In fixing this I want to create a test for that situation but I’m unsure how to proceed. It seems not as easy as creating the object and testing each member as this can pass the test depending on whether the autorelease pool decided to let go of the object right away or not.
My question is: Is there a way I can create the object and assure that any autorelease happens before I do my tests? Should I create my own pool in the test case and release before I preform that ivar testing? Will that catch this case 100% of the time? Should I wrap ALL my tests in an autorelease pool to proactively catch any potential crashes?
If you use a constructor that gives you a non-autoreleased object, then you don’t have to worry about this. But otherwise, you do need to use autorelease pools.
This will get you an EXC_BAD_ACCESS immediately if and only if
arrayis a not retained by the setter, since its retain count goes to 0 on thereleaseand it gets immediately deallocated:Whereas this wouldn’t cause any errors, even though
self.arraywould become garbage later on, as soon as the autorelease pool was drained:If you have to use a convenience constructor, just wrap the assignment in an autorelease pool:
This way you’ll get an EXC_BAD_ACCESS again as long as
arrayisn’t retained by the setter.