对象内存布局

2020-09-13  本文已影响0人  miaohy

初始化一个对象

LGPerson *objc2 = [[LGPerson alloc] init];

对象没初始化之前,调试窗口执行x/4gx objc2
输出如下:

0x7ffeefbff578: 0x0000000000000000 0x0000000000000001
0x7ffeefbff588: 0x00007ffeefbff750 0x0000000000000000

可见栈地址里面放的还是空的
执行代码后,调试窗口执行x/4gx objc2

0x10072d370: 0x001d80010000218d 0x0000000000000000
0x10072d380: 0x72616553534e5b2d 0x20646c6569466863

为什么会如此呢?这中间经历了什么呢?

po 0x10072d370
<LGPerson: 0x10072d370 >

由此可见目前生成了一个LGPerson实例对象,并且所在内存空间为0x10072d370
既然LGPerson存放在0x10072d370,那0x10072d370地址里面到底放的是什么,对象是以什么状态存在的呢?

(lldb) x/4gx objc2  //里面有一个isa指针,实例对象没有superclass
0x10072d370: 0x001d80010000218d 0x0000000000000000
0x10072d380: 0x72616553534e5b2d 0x20646c6569466863

(lldb) p/x 0x001d80010000218d & 0x0000000ffffffff8ULL
(unsigned long long) $5 = 0x0000000100002188
(lldb) po 0x0000000100002188
LGPerson
(lldb) x/4gx 0x0000000100002188 //类对象isa指针指向元类,父类指针指向NSObject
0x100002188: 0x0000000100002160 0x0000000100334140
0x100002198: 0x0000000101166ff0 0x0004801c00000007

(lldb) x/4gx 0x0000000100002160 //元类里的isa指针指向NSObject的元类,父类指针指向NSObject的元类
0x100002160: 0x00000001003340f0 0x00000001003340f0
0x100002170: 0x000000010072eaa0 0x0003e03500000007
(lldb) po 0x100002160
LGPerson

(lldb) po 0x00000001003340f0
NSObject

(lldb) po [NSObject alloc]
<NSObject: 0x10115f5e0>

(lldb) po NSObject.class
NSObject

(lldb) p/x NSObject.class
(Class) $11 = 0x0000000100334140 NSObject
(lldb) x/4gx 0x0000000100334140 //NSObject的isa指针指向NSObject的元类,父类指针指向nil
0x100334140: 0x00000001003340f0 0x0000000000000000
0x100334150: 0x0000000100653d10 0x0004801000000007
(lldb) po 0x00000001003340f0
NSObject

(lldb) x/4gx 0x00000001003340f0 //可见nsobject的元类的isa指针指向nsobject的元类自己,父类指针指nsobject类对象
0x1003340f0: 0x00000001003340f0 0x0000000100334140
0x100334100: 0x0000000101167460 0x0005e0310000000f
WeChat57d3a692d3df3c230d7ae9a7208b18d7.png
WeChatcaf874e37e31f7400813aad930c2bc31.png

查看实例方法在内存中情况

(lldb) x/4gx LGSonPerson.class
0x1000022b8: 0x0000000100002290 0x0000000100002268
0x1000022c8: 0x000000010032e430 0x0000801c00000000

(lldb) p (class_data_bits_t*)0x1000022d8
(class_data_bits_t *) $3 = 0x00000001000022d8
(lldb) p $3->data()
(class_rw_t *) $4 = 0x00000001007060e0
(lldb) p *$4
(class_rw_t) $5 = {
  flags = 2148007936
  witness = 0
  ro_or_rw_ext = {
    std::__1::atomic<unsigned long> = 4294975936
  }
  firstSubclass = nil
  nextSiblingClass = nil
}

(lldb) p $5.properties()
(const property_array_t) $7 = {
  list_array_tt<property_t, property_list_t> = {
     = {
      list = 0x0000000000000000
      arrayAndFlag = 0
    }
  }
}
(lldb) p $5.methods()
(const method_array_t) $8 = {
  list_array_tt<method_t, method_list_t> = {
     = {
      list = 0x0000000100002208
      arrayAndFlag = 4294976008
    }
  }
}

(lldb) p $8.list
(method_list_t *const) $9 = 0x0000000100002208

(lldb) p *$9
(method_list_t) $11 = {
  entsize_list_tt<method_t, method_list_t, 3> = {
    entsizeAndFlags = 26
    count = 1
    first = {
      name = "sayhello"
      types = 0x0000000100000f9a "v16@0:8"
      imp = 0x0000000100000e80 (KCObjc`-[LGSonPerson sayhello])
    }
  }
}
(lldb) p $11.get(0)
(method_t) $12 = {
  name = "sayhello"
  types = 0x0000000100000f9a "v16@0:8"
  imp = 0x0000000100000e80 (KCObjc`-[LGSonPerson sayhello])
}

类方法在内存中布局

(lldb) x/4gx 0x0000000100002290
0x100002290: 0x00000001003340f0 0x0000000100002240
0x1000022a0: 0x00000001011077a0 0x0002e03500000003

(lldb) p (class_data_bits_t*)0x1000022b0
(class_data_bits_t *) $14 = 0x00000001000022b0

(lldb) p $14->data()
(class_rw_t *) $16 = 0x0000000101161350
(lldb) p *$16
(class_rw_t) $17 = {
  flags = 2684878849
  witness = 1
  ro_or_rw_ext = {
    std::__1::atomic<unsigned long> = 4294975832
  }
  firstSubclass = nil
  nextSiblingClass = nil
}
(lldb) p $17.properties()
(const property_array_t) $18 = {
  list_array_tt<property_t, property_list_t> = {
     = {
      list = 0x0000000000000000
      arrayAndFlag = 0
    }
  }
}
(lldb) p $17.methods()
(const method_array_t) $19 = {
  list_array_tt<method_t, method_list_t> = {
     = {
      list = 0x00000001000021a0
      arrayAndFlag = 4294975904
    }
  }
}
(lldb) p $19.list
(method_list_t *const) $20 = 0x00000001000021a0
(lldb) p $20
(method_list_t *const) $20 = 0x00000001000021a0
(lldb) p *$20
(method_list_t) $21 = {
  entsize_list_tt<method_t, method_list_t, 3> = {
    entsizeAndFlags = 26
    count = 1
    first = {
      name = "say"
      types = 0x0000000100000f87 "@16@0:8"
      imp = 0x0000000100000e60 (KCObjc`+[LGSonPerson say])
    }
  }
}

上一篇 下一篇

猜你喜欢

热点阅读