Any way to check if an instance is still in memory?
-
22-09-2019 - |
문제
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?
해결책
You need to read this again:
Cocoa Memory Management Guidelines
In short, if you want something to stick around you must retain
it.
If you want something to go away and you have previously retained it, you must release
or autorelease
it.
You must never call dealloc
directly (except [super dealloc];
at the end of every one of your dealloc
methods).
You must never release
or autorelease
an object that you did not retain
.
Note that some methods do return retained objects that you must release
. If you alloc
an instance of a class, that implies a retain
. If you copy
and instance, the copy is retained.
If you are ever tempted to use the retainCount
method, 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.
다른 팁
가 있는지 확인하십시오.
- 팀 사이트 (다른 사이트 템플릿과 함께 작동하지 않을 수도 있음)
- 엔터프라이즈 기능 사용
- PowerPoint 2010
커서가보고있는 필드를 얻으려면 반환 된 문서에 가져 오기를 사용할 수 있습니다.다음과 같이 :
System.out.println(cursor.next().get("key"));
. If by variable, you mean whether the pointer to your object still references valid memory then:
MyClass *myVariable = [[MyClass alloc] init];
//Tons of stuff happens...
if (myVariable != nil) //Do more stuff