• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Any way to tell if a pointer is an object?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Any way to tell if a pointer is an object?


  • Subject: Re: Any way to tell if a pointer is an object?
  • From: Kirk Kerekes <email@hidden>
  • Date: Sun, 25 Feb 2007 11:53:45 -0600


------------------------------

Message: 16
Date: Sat, 24 Feb 2007 20:19:54 -0800
From: Jerry Krinock <email@hidden>
Subject: Any way to tell if a pointer is an object?
To: Cocoa Developers <email@hidden>
Message-ID: <C2064E6A.2E6DC%email@hidden>
Content-Type: text/plain;	charset="US-ASCII"

When debugging, sometimes I get a pointer that may or may point to an
object. I would like to know what it is. If I send a message such as
-description to it, and my app crashes, I conclude that, "Gee, I guess it
does not point an object".


Is there a less destructive and more satisfying way to find out whether or
not an arbitrary pointer points to an object?


Thanks,

Jerry Krinock

Try the following, which may be worth exactly what you paid for it.

The logic is that the inner routine makes a best-effort stab at identifying invalid or deallocated pointers. If those tests pass (or cannot be made) the outer routine looks for an isa pointer and runs it through the same gauntlet. If both pass, the odds are pretty good that you are looking at a functioning object reference.

All valid object pointers should pass this test, and failure to pass can be considered an absolute indicator, however it cannot be guaranteed that an arbitrary invalid pointer will fail. The odds on a random pointer passing both the test on itself _and_ on its inferred isa field would seem slim, however. You can enhance the odds by testing further up the isa chain, or adding other heuristics that take advantage of the define Objc object structure. This version stops here because it has yet to produce erroneous results for me, despite the superficiality of the tests.

extern Class _objc_getFreedObjectClass(void) __attribute__ ((weak_import));
#import </usr/include/malloc/malloc.h>


BOOL _IsProbablyValid_Obj_Pointer(void * ptr)
{
	BOOL result = YES;
	BOOL staticData = (malloc_zone_from_ptr(ptr) == NULL);
	static Class freedObjectClass = nil;

	if(!freedObjectClass)
	   {
		if(!(freedObjectClass = _objc_getFreedObjectClass()))
			{
			NSLog(@"\n_objc_getFreedObjectClass not available!");
			}
	   }
		

if
(
(!ptr) || /* NIL ptr */
(ptr == freedObjectClass) || /* freed or nil */
((uintptr_t)ptr & (staticData ? 0x03 : 0x0F)) /* invalid pointer target */
)
{
result = NO;
}

return result;
}



BOOL IsProbablyValidObjectPointer(id obj) { BOOL result = YES; if((result = _IsProbablyValid_Obj_Pointer(obj))) { void * isa = (*obj).isa; result = IsProbablyValid_Obj_Pointer(isa); } return result; } _______________________________________________

Cocoa-dev mailing list (email@hidden)

Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Prev by Date: C *char representation of PDF into NSImage
  • Next by Date: [ANN] Drop-Installable Subversion Server
  • Previous by thread: Re: Any way to tell if a pointer is an object?
  • Next by thread: Parsing STDout into an NSDictionary
  • Index(es):
    • Date
    • Thread