Example: I have a view controller and get rid of it. But there’s still an variable holding it’s memory address. Accessing that results in EXEC_BAD_ACCESS. Of course. But: Is there any way to check if that variable is still valid? i.e. if it’s still pointing to something that exists in memory?
Share
You need to read this again:
Cocoa Memory Management Guidelines
In short, if you want something to stick around you must
retainit.If you want something to go away and you have previously retained it, you must
releaseorautoreleaseit.You must never call
deallocdirectly (except[super dealloc];at the end of every one of yourdeallocmethods).You must never
releaseorautoreleasean object that you did notretain.Note that some methods do return retained objects that you must
release. If youallocan instance of a class, that implies aretain. If youcopyand instance, the copy is retained.If you are ever tempted to use the
retainCountmethod, don’t. It isn’t useful. Only consider retain counts as a delta; if you add, you must subtract, but the absolute value is an implementation detail that should be ignored.(In other words, even if there were ways to check for an object’s validity definitively — there aren’t — it would be the wrong answer.)
Oh, and use the Build and Analyze feature in Xcode. It does a very good — but not quite perfect — job of identifying memory management problems, amongst other things.