1

So this question may be invalid, depending on my understanding,

But let's say we have an object on the heap, say at 0x618000001234

If I use Xcode's memory debugger, to view the memory at that address, I just see a bunch of information, right?

Now as far as my understanding goes, the first thing you will find at that address is the isa pointer, which is a Class AKA objc_class

and it looks like that just takes up 8 bytes enter image description here

So in this example: enter image description here

It looks like the contents of isa is: 011DFFFF9C1B89F9

Great, fine and dandy

So let's say we have a class

@interface testClass : NSObject
@property int testIntProperty;
@end

Assuming there is nothing in memory after the isa, and it goes straight into the properties, if we looked at the location in memory, of an instance of testClass, I would expect the first 8 bytes to be the isa, then the next 4 bytes to be the int property, and then the rest doesn't matter

So my question is: In that example, we can tell that the testIntProperty will be located 8 bytes from the start of the object, just by looking at the header of the property

But is there a way to tell exactly where it's going to be, say if there are 10 properties, and you want to know where the 10th property is?

The reason why I'm asking, is because I want to look at a specific property's pointer, and I'm having a hard time finding it in memory. At runtime, I get an EXC_BAD_ACCESS, with address 0x18, but I'm curious to actually look in memory at the location I expect that property to exist at, and I want to see the 0x18 in plain sight

A O
  • 5,516
  • 3
  • 33
  • 68
  • Take a look at: https://stackoverflow.com/questions/119123/why-isnt-sizeof-for-a-struct-equal-to-the-sum-of-sizeof-of-each-member – ninjaproger Jul 05 '17 at 23:20
  • 1
    You can check the value of the property in the debugger with `call (int)[0x618000001234 testIntProperty]`. If the property is a pointer to an object, use `po [0x618000001234 testObjectProperty]`. – Willeke Jul 06 '17 at 08:52

1 Answers1

0

In the modern runtime you cannot assume the layout of a class is just the instance variables/properties in order; you can declare these in more than one place - interface and implementation, you have to know what padding is used, you don't know when property backing variables are processed, etc.

However you can easily determine the offset in the debugger. Assuming you are in an instance method of your testClass so self references it enter:

p (char *)&self->_testIntProperty - (char *)self

and it will print the offset. The casts are required to keep the debugger happy.

HTH

CRD
  • 52,522
  • 5
  • 70
  • 86