2

I am really confused with this subject. I am using LLVM 4.1 compiler on Xcode and when I compile a simple basic code like this in 32 bit or x64 or x64-x86 mode with ARC off, everything is ok but if I compile with Automatic Reference Counting mode; I receive BAD_ACCESS error.

This is a sample code:

-(void) doNothing{

__asm__ volatile(
             "pushl %ebp;"
             "movl %esp , %ebp ;" 
             "movl %eax , -4(%ebp);"
             "movl %ebp, %esp;"
             "popl %ebp;"
             "leave;" 
             "ret  "    // retl in x86
             );

}

I tried to figure out the source of error I found that the push/pops are the only parts that work in ARC mode!

It is so frustrating because I have written a huge code in past in inline assembly and now I have to joint it to a module with ARC. Now if I can't solve it , I may need to revise all that module for working without ARC !

Can anyone help me on that?

phuclv
  • 37,963
  • 15
  • 156
  • 475
Aug
  • 595
  • 9
  • 22

4 Answers4

2

There's probably a solution, but provided you can't fix it, you can move the problematic methods' implementation into a category and compile just that category with -fno-objc-arc. That way your objc code remains arc'ed and you get your assembler working

Fernando Mazzon
  • 3,562
  • 1
  • 19
  • 21
  • Thanks. but unfortunately I have recently moved from visual studio into Xcode. I dont know where I should access the compiler settings for setting to -fno-objc-arc. I would appreciate if you could help me on that – Aug Jan 03 '13 at 20:03
  • 1
    Click on the project (top left, blue thing), then select your target on the right, build phases, Compile sources. Double click on the file you want to de-arc, and -fno-objc-arc it :) – Fernando Mazzon Jan 03 '13 at 20:07
  • The same problem ! but at least I learned to work with Xcode compiler. thanks for that. I think something is wrong with my code.It seems that after the line "movl %esp , %ebp ;" the ARC loses the primary stack pointer address. can you have a look at the code itself please? – Aug Jan 03 '13 at 20:26
  • I haven't written assembly since 2009 and I was never that well versed. Sorry, I'm useless to you for that haha. – Fernando Mazzon Jan 03 '13 at 20:28
2

I suggest trying to put assembler code in C or C++ function, not the ObjC message. And adding __attribute__((naked)) to function declaration.

Mārtiņš Možeiko
  • 12,733
  • 2
  • 45
  • 45
2

You cannot use ARC in 32-bit mode on OS X. According to the Objective-C Feature Availability Index, ARC requires a 64 system on OS X.

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
0

I found the solution but dont know why it works!! I will give the point of answer to whom can explain it. I realized that the main cause of problem is related to x64 behaviour of ARC. So I changed the code &deleted the lines "leave" and "ret" and now it works but I dont know : 1- why ARC behaves like x64 even in x86 mode and 2- why I had to omit "leave" and "ret"new code:

             "pushq %rbp;"
             "movq %rsp , %rbp ;"
             "movl %eax , -4(%rbp);"
             "popq %rbp;"
             );
phuclv
  • 37,963
  • 15
  • 156
  • 475
Aug
  • 595
  • 9
  • 22
  • 1
    What do you mean by "ARC in x86 mode"? According to the [Objective-C Feature Availability Index](http://developer.apple.com/library/ios/#releasenotes/ObjectiveC/ObjCAvailabilityIndex/_index.html%23//apple_ref/doc/uid/TP40012243), ARC requires a 64 system on OS X. – Martin R Jan 04 '13 at 05:54
  • thanks I didn't know that . you could put it as an answer instead of a comment that I could mark it as correct answer. – Aug Jan 04 '13 at 08:03