Objective-C, Ivar - object_getIvar() question

I am working on a personal debug tool to access an object's instance variables. I have been able to extract necessary object data with runtime 's Ivar functions with the EXCEPTION of c-arrays, encode types '[' and ']'. In those two cases, the function object_getIvar() only (correctly) delivers the first two array elements ([0],[1]). A memory display of the Ivar shows that the ivar data (extracted from the ivar_list) only contains the data for the FIRST TWO elements. I have found no helpful clues in the doc's, (runtime.h) header, or WWW searches. The Xcode debugger can display the data correctly, it must be somewhere.

Can anyone provide suggestions as two how to access the full list of data?

Best Answer

  • ksteff
    ksteff 🤠
    Answer ✓

    Solved (I believe)

    Experimentation revealed that if the code used object_getIvar(iVar) only the first two object data elements in multi element objects (c-arrays, structs, etc) were referenced (via pointer). A generic (id) pointer to scalar data like char *, int, etc. as well as object references worked fine with object_getIvar().

    By making an address reference from the address of {your object} and adding ivar_getOffset(ivar) a pointer to the first of an entire list of elements is provided (e.g. ele[0]). Using the objCType and the item count from the ivar provides what you need to define the entire area.

    I do not know the exact layout of the object's ivar area but I suspect that larger static ivar memory elements are allocated in a different area (memory pool?). The object_getIvar functions appears to not handle that situation ivar_getOffset does.

    I have a working example if that helps, a bit of effort would be needed to make one public.