Re: runtime and type encoding confusion on 64-bit

Christiaan Hofman <[email protected]>
Newsgroups gmane.comp.macosx.devel
Message-ID <[email protected]>
On Sep 5, 2009, at 18:41, Shantonu Sen wrote:

> 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

Ah, yea, that confused me as well. But believe it or not: your  
solution does not work. However, mine does and returns exactly the  
result it should give, consistently!

I think that means that the method that writes to this ivar is  
actually making the same mistake, and is writing the value to the  
address rather than the ivar itself. That's a really serious mistake!  
And as the framework in question is loaded with object that must be  
accessed in this way, I now completely lost my trust in it. Full  
disclosure: the framework is called PDFKit. I'm ever more surprised  
Preview actually works in 64-bit!

Christiaan

>
> 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
>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.