Re: cocoa-dev digest, Vol 2 #3103 - 18 msgs
Re: cocoa-dev digest, Vol 2 #3103 - 18 msgs
- Subject: Re: cocoa-dev digest, Vol 2 #3103 - 18 msgs
- From: p3consulting <email@hidden>
- Date: Wed, 29 Oct 2003 09:50:16 +0100
is there anyway to test if a pointer, that isn't NULL, is pointing to
a valid object or not? when you have a valid pointer to an object,
that pointer points to the isa of that instance. that isa points to
the class that it's an instance of. so i tried:
if( [object isKindOfClass:[NSObject class]] )
but that crashed when it wasn't a valid object. is there anyway to
safely test for a valid object?
Not with non-debug code. It isn't possible because you don't know what
is in the memory previously occupied by the object... it might very
well be a valid object, in which case you're going to send a message to
some other object in your application. Or it might look
(superficially) like a valid object, but actually be something else
entirely, in which case you'll probably crash when you try to call a
method.
It''s not possible that way because calling isKindOfClass assume
the target is an object
In other words the hypothesis has been used in the
demonstration making it void.
Theory of operation for a IsValidObjectPointer(void *ptr)
function: (may I insist on the "theory" word here)
1. if p is not a multiple of 4 it''s not a valid mallocated
pointer: reject it
2. check that p is in the memory space (some Unix/Mach/BSD calls
should provide this kind of info) of your process and - if this is
possible - check if it is in the malloc heap
3. if ok then dereferences it (it's now guaranted not to crash your
code with a signal) to get the theorical isa and loop on that
process
(checking if the isa you now have is correctly aligned, in your
memory space etc.)
until you reach the top of the hierarchy that should be a
[NSObject class]
NB
be careful on the fact that a valid mallocated pointer may be on a data
structure that has nothing to with an Objective-C class and by
consequence may contain cyclic references
p = malloc(sizeof(int)); *p = p;
is a trivial example of what I mean
You should protect your code against that or be sure you never call it
with a variable that's not susceptible to contain or have contained an
Obj-C object at any time in its life !
To answer the orginal question:
it should be possible,
but it's not trivial and will introduce a significant overhead in your
program if you use it for anything else than debugging purpose
If the use if strictly for debugging purpose you could also "patch"
dealloc to make it erase the deallocated memory with a known pattern
before actually freeing it
(or link against a debugging malloc library that will do it for you),
this will make writing the loop "searching for the topmost isa" a
little bit easier to write
Pascal Pochet
P3 Consulting
email@hidden
http://www.p3-consulting.net
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.