初始化一個(gè)對(duì)象
LGPerson *objc2 = [[LGPerson alloc] init];
對(duì)象沒初始化之前恐似,調(diào)試窗口執(zhí)行x/4gx objc2
輸出如下:
0x7ffeefbff578: 0x0000000000000000 0x0000000000000001
0x7ffeefbff588: 0x00007ffeefbff750 0x0000000000000000
可見棧地址里面放的還是空的
執(zhí)行代碼后锻离,調(diào)試窗口執(zhí)行x/4gx objc2
0x10072d370: 0x001d80010000218d 0x0000000000000000
0x10072d380: 0x72616553534e5b2d 0x20646c6569466863
為什么會(huì)如此呢穷躁?這中間經(jīng)歷了什么呢仇让?
po 0x10072d370
<LGPerson: 0x10072d370 >
由此可見目前生成了一個(gè)LGPerson實(shí)例對(duì)象那槽,并且所在內(nèi)存空間為0x10072d370
既然LGPerson存放在0x10072d370蜈膨,那0x10072d370地址里面到底放的是什么屿笼,對(duì)象是以什么狀態(tài)存在的呢?
(lldb) x/4gx objc2 //里面有一個(gè)isa指針翁巍,實(shí)例對(duì)象沒有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 //類對(duì)象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類對(duì)象
0x1003340f0: 0x00000001003340f0 0x0000000100334140
0x100334100: 0x0000000101167460 0x0005e0310000000f
查看實(shí)例方法在內(nèi)存中情況
(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])
}
類方法在內(nèi)存中布局
(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])
}
}
}