Re: runtime and type encoding confusion on 64-bit
Shantonu Sen <[email protected]>
| Newsgroups | gmane.comp.macosx.devel |
|---|---|
| Message-ID | <[email protected]> |
Did you look at the prototype? OBJC_EXPORT Ivar object_getInstanceVariable(id obj, const char *name, void **outValue); "&count" is not a void **, despite your cast. It probably returns you the address of the ivar. Does it actually work when you use NSUInteger, or does it just fail to crash? If it saved the ivar value to the pointer you passed in, it would never work for structures and floating point numbers that vary in size. Try something like unsigned int count; unsigned int *countptr; object_getInstanceVariable(obj, "count", &countptr); count = *countptr; Shantonu Sent from my MacBook On Sep 5, 2009, at 9:18 AM, Christiaan Hofman wrote: > I am trying to understand what's going wrong in a weird class for > which I'm trying to use runtime functions to access ivars (as the > class doesn't define accessors). The context is a 64-bit executable > (which is relevant). > > The class has an ivar called (for this argument) "count" of type > "unsigned int" (ivar_getTypeEncoding() returns "I" and also class- > dump shows it as "unsigned int", yes I'm talking about arch=x86_64), > so I'm trying to get the value as follows: > > unsigned int count; > object_getInstanceVariable(obj, "count", (void *)&count); > > This crashes with an "EXC_BAD_ACCESS" signal. > > However when I do the following it works: > > NSUInteger count; > object_getInstanceVariable(obj, "count", (void *)&count); > > This makes no sense to me, as this is the wrong type. > > Does anyone understand why this can work when I do it wrong and does > not work when I do it right? > > I do know that the type of the ivar probably should be > NSUInteger=long long for 64 bits, but that's not my fault (it's not > my class). > > Thanks, > Christiaan > > _______________________________________________ > MacOSX-dev mailing list > [email protected] > http://www.omnigroup.com/mailman/listinfo/macosx-dev