在ARC下,使用运行时方法分配给对象类型的ivar是否合法/安全?

时间:2013-03-02 15:28:10

标签: objective-c automatic-ref-counting variable-assignment objective-c-runtime ivar

基于technique described here我使用ivar_getOffset()方法在对象实例中设置ivars。

现在我的情况是ivar是NSString *:

NSString* _name;

更新

我在错误的轨道上(在这种情况下不能使用ivar_getOffset),我应该使用:

object_setIvar(target, _ivar, [stringValue copy]);

将对象值分配给ivars。

我的问题仍然存在:在ARC下这样做是否安全?是否可以通过ARC正确释放ivar现有的物体?

我担心ARC在分配之前可能无法正确释放ivar设置的任何字符串。虽然我对此表示怀疑,否则编译器不会让我喜欢object_setInstanceVariable(“在自动引用计数模式下不可用”) - 对吗?

2 个答案:

答案 0 :(得分:7)

是的,使用ARC执行此操作是合法的,至少在Mac OS X 10.8.2中是合法的。

如果你要反汇编libobjc,你会发现:

================ B E G I N   O F   P R O C E D U R E ================


                                       ; Basic Block Input Regs: rdx rsi rdi -  Killed Regs: rbx rbp r15
                                            _object_setIvar:
000000000000c1f5 55                              push       rbp                           ; XREF=0xc1e1
000000000000c1f6 4889E5                          mov        rbp, rsp
000000000000c1f9 4157                            push       r15
000000000000c1fb 4156                            push       r14
000000000000c1fd 4155                            push       r13
000000000000c1ff 4154                            push       r12
000000000000c201 53                              push       rbx
000000000000c202 4883EC18                        sub        rsp, 0x18
000000000000c206 488955C8                        mov        qword [ss:rbp-0x40+var_8], rdx
000000000000c20a 4889F3                          mov        rbx, rsi
000000000000c20d 4989FF                          mov        r15, rdi
000000000000c210 4D85FF                          test       r15, r15
000000000000c213 0F8449010000                    je         0xc362
                                       ; Basic Block Input Regs: rbx -  Killed Regs: <nothing>
000000000000c219 4885DB                          test       rbx, rbx
000000000000c21c 0F8440010000                    je         0xc362
                                       ; Basic Block Input Regs: r15 -  Killed Regs: rax
000000000000c222 41F6C701                        test       r15L, 0x1
000000000000c226 4C89F8                          mov        rax, r15
000000000000c229 740F                            je         0xc23a
                                       ; Basic Block Input Regs: <nothing> -  Killed Regs: rax
000000000000c22b 4883E00F                        and        rax, 0xf
000000000000c22f 48C1E003                        shl        rax, 0x3
000000000000c233 4803057ECE1000                  add        rax, qword [ds:0x1190b8]
                                       ; Basic Block Input Regs: rax rbx -  Killed Regs: rdx rbp rsi rdi r12
000000000000c23a 4C8B20                          mov        r12, qword [ds:rax]           ; XREF=0xc229
000000000000c23d 48C745D000000000                mov        qword [ss:rbp-0x40+var_16], 0x0
000000000000c245 4889DF                          mov        rdi, rbx
000000000000c248 E805F5FFFF                      call       _ivar_getName
000000000000c24d 488D55D0                        lea        rdx, qword [ss:rbp-0x40+var_16]
000000000000c251 4C89E7                          mov        rdi, r12
000000000000c254 4889C6                          mov        rsi, rax
000000000000c257 E8C9E7FFFF                      call       __class_getVariable
000000000000c25c 4885C0                          test       rax, rax
000000000000c25f 7433                            je         0xc294
                                       ; Basic Block Input Regs: <nothing> -  Killed Regs: r14
000000000000c261 4C8D75D0                        lea        r14, qword [ss:rbp-0x40+var_16]
000000000000c265 EB1F                            jmp        0xc286
                                       ; Basic Block Input Regs: rax rbx r12 r14 -  Killed Regs: rdx rbp rsi rdi
000000000000c267 E88EC3FFFF                      call       __class_getSuperclass         ; XREF=0xc292
000000000000c26c 488945D0                        mov        qword [ss:rbp-0x40+var_16], rax
000000000000c270 4889DF                          mov        rdi, rbx
000000000000c273 E8DAF4FFFF                      call       _ivar_getName
000000000000c278 4C89E7                          mov        rdi, r12
000000000000c27b 4889C6                          mov        rsi, rax
000000000000c27e 4C89F2                          mov        rdx, r14
000000000000c281 E89FE7FFFF                      call       __class_getVariable
                                       ; Basic Block Input Regs: rax rbx -  Killed Regs: <nothing>
000000000000c286 4839D8                          cmp        rax, rbx                      ; XREF=0xc265
000000000000c289 7409                            je         0xc294
                                       ; Basic Block Input Regs: rbp -  Killed Regs: rdi
000000000000c28b 488B7DD0                        mov        rdi, qword [ss:rbp-0x40+var_16]
000000000000c28f 4885FF                          test       rdi, rdi
000000000000c292 75D3                            jne        0xc267
                                       ; Basic Block Input Regs: rax rbx rbp -  Killed Regs: rbx rdi r12 r13
000000000000c294 4C8B6DD0                        mov        r13, qword [ss:rbp-0x40+var_16] ; XREF=0xc25f, 0xc289
000000000000c298 4889DF                          mov        rdi, rbx
000000000000c29b E8B3F2FFFF                      call       _ivar_getOffset
000000000000c2a0 4889C3                          mov        rbx, rax
000000000000c2a3 4D8D241F                        lea        r12, qword [ds:r15+rbx]
000000000000c2a7 4C89EF                          mov        rdi, r13
000000000000c2aa E8B3F2FFFF                      call       __class_usesAutomaticRetainRelease
000000000000c2af 84C0                            test       al, al
000000000000c2b1 746B                            je         0xc31e
                                       ; Basic Block Input Regs: rax r13 -  Killed Regs: rdi r14
000000000000c2b3 4C89EF                          mov        rdi, r13
000000000000c2b6 E840A00000                      call       __class_getInstanceStart
000000000000c2bb 4189C6                          mov        r14d, eax
000000000000c2be 4C89EF                          mov        rdi, r13
000000000000c2c1 E80EA80000                      call       _class_getWeakIvarLayout
000000000000c2c6 4885C0                          test       rax, rax
000000000000c2c9 7423                            je         0xc2ee
                                       ; Basic Block Input Regs: rax rbx r14 -  Killed Regs: rcx rsi rdi
000000000000c2cb 4489F1                          mov        ecx, r14d
000000000000c2ce 4889DF                          mov        rdi, rbx
000000000000c2d1 4829CF                          sub        rdi, rcx
000000000000c2d4 4889C6                          mov        rsi, rax
000000000000c2d7 E8483C0000                      call       __ZL17is_scanned_offsetlPKh   ; is_scanned_offset(long, unsigned char const*)
000000000000c2dc 84C0                            test       al, al
000000000000c2de 740E                            je         0xc2ee
                                       ; Basic Block Input Regs: rbp r12 -  Killed Regs: rsi rdi
000000000000c2e0 4C89E7                          mov        rdi, r12
000000000000c2e3 488B75C8                        mov        rsi, qword [ss:rbp-0x40+var_8]
000000000000c2e7 E82B440100                      call       _objc_storeWeak
000000000000c2ec EB74                            jmp        0xc362
                                       ; Basic Block Input Regs: rax r13 -  Killed Regs: rdi
000000000000c2ee 4C89EF                          mov        rdi, r13                      ; XREF=0xc2c9, 0xc2de
000000000000c2f1 E8C5A70000                      call       _class_getIvarLayout
000000000000c2f6 4885C0                          test       rax, rax
000000000000c2f9 7423                            je         0xc31e
                                       ; Basic Block Input Regs: rax rbx r14 -  Killed Regs: rcx rsi rdi
000000000000c2fb 4489F1                          mov        ecx, r14d
000000000000c2fe 4889DF                          mov        rdi, rbx
000000000000c301 4829CF                          sub        rdi, rcx
000000000000c304 4889C6                          mov        rsi, rax
000000000000c307 E8183C0000                      call       __ZL17is_scanned_offsetlPKh   ; is_scanned_offset(long, unsigned char const*)
000000000000c30c 84C0                            test       al, al
000000000000c30e 740E                            je         0xc31e
                                       ; Basic Block Input Regs: rbp r12 -  Killed Regs: rsi rdi
000000000000c310 4C89E7                          mov        rdi, r12
000000000000c313 488B75C8                        mov        rsi, qword [ss:rbp-0x40+var_8]
000000000000c317 E8044C0100                      call       _objc_storeStrong
000000000000c31c EB44                            jmp        0xc362
                                       ; Basic Block Input Regs: <nothing> -  Killed Regs: <nothing>
000000000000c31e 803DEB49110000                  cmp        byte [ds:_UseGC], 0x0         ; XREF=0xc2b1, 0xc2f9, 0xc30e
000000000000c325 7428                            je         0xc34f
                                       ; Basic Block Input Regs: rax r13 -  Killed Regs: rdi
000000000000c327 4C89EF                          mov        rdi, r13
000000000000c32a E8A5A70000                      call       _class_getWeakIvarLayout
000000000000c32f 4885C0                          test       rax, rax
000000000000c332 741B                            je         0xc34f
                                       ; Basic Block Input Regs: rax rbx -  Killed Regs: rsi rdi
000000000000c334 4889DF                          mov        rdi, rbx
000000000000c337 4889C6                          mov        rsi, rax
000000000000c33a E8E53B0000                      call       __ZL17is_scanned_offsetlPKh   ; is_scanned_offset(long, unsigned char const*)
000000000000c33f 84C0                            test       al, al
000000000000c341 740C                            je         0xc34f
                                       ; Basic Block Input Regs: rbp r12 -  Killed Regs: rsi rdi
000000000000c343 488B7DC8                        mov        rdi, qword [ss:rbp-0x40+var_8]
000000000000c347 4C89E6                          mov        rsi, r12
000000000000c34a E831230000                      call       _objc_assign_weak
                                       ; Basic Block Input Regs: rbx rbp r15 -  Killed Regs: rax rdx rsi rdi
000000000000c34f 488D055AE51000                  lea        rax, qword [ds:_objc_assign_ivar_internal] ; XREF=0xc325, 0xc332, 0xc341
000000000000c356 488B7DC8                        mov        rdi, qword [ss:rbp-0x40+var_8]
000000000000c35a 4C89FE                          mov        rsi, r15
000000000000c35d 4889DA                          mov        rdx, rbx
000000000000c360 FF10                            call       qword [ds:rax]
                                       ; Basic Block Input Regs: <nothing> -  Killed Regs: rbx rsp rbp r12 r13 r14 r15
000000000000c362 4883C418                        add        rsp, 0x18                     ; XREF=0xc213, 0xc21c, 0xc2ec, 0xc31c
000000000000c366 5B                              pop        rbx

请注意_ 000000000000c2aa处_class_usesAutomaticRetainRelease的调用。

此方法的实施可在opensource.apple.com

处获得

此函数的标题注释是:

/***********************************************************************
 * _class_usesAutomaticRetainRelease
 * Returns YES if class was compiled with -fobjc-arc
 **********************************************************************/

这意味着ObjC运行时的这个特定功能可以识别ARC。

答案 1 :(得分:2)

我不知道答案,但我知道如何确定答案...

创建一个指向您自己的类实例的ivar,例如LC2DFoo。在-[LC2DFoo dealloc]中放置一个断点。创建LC2DFoo的实例,并使用您描述的运行时方法将其分配给ivar。接下来,将相同的ivar设置为其他内容,例如nil。你打破了断点吗?