Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Under ARC, is it legal/safe to assign to an object-type ivar using runtime methods?

Based on the technique described here I'm setting ivars in object instances using the ivar_getOffset() method.

Now I have the case where the ivar is a NSString*:

NSString* _name;

UPDATE:

I was on the wrong track (can't use ivar_getOffset in this case), I should be using:

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

to assign object values to ivars.

My question remains: is this safe to do under ARC? Will the ivar's existing object be properly released by ARC?

I'm worried that ARC might not properly release whatever string the ivar was set to before the assignment. Though I doubt it, otherwise the compiler wouldn't let me like in the case of object_setInstanceVariable ("not available in automatic reference counting mode") - right?

like image 636
LearnCocos2D Avatar asked Jan 15 '23 01:01

LearnCocos2D


2 Answers

Yes, doing this with ARC is legal, at least in Mac OS X 10.8.2

If you would disassemble libobjc, you would find this:

================ 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

Note the call of _class_usesAutomaticRetainRelease at 000000000000c2aa.

The implementation of this method is available at opensource.apple.com

The header comment to this functions is:

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

Which means this particular function of ObjC runtime is aware of ARC.

like image 161
Kentzo Avatar answered Jan 19 '23 12:01

Kentzo


I don't know the answer, but I know how to determine the answer...

Create an ivar that points to an instance of a class of your own, like LC2DFoo. Put a breakpoint in -[LC2DFoo dealloc]. Create an instance of LC2DFoo and assign it to the ivar using the runtime method you describe. Next, set the same ivar to something else, like nil. Do you hit your breakpoint?

like image 20
Caleb Avatar answered Jan 19 '23 13:01

Caleb