• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Strange property behavior
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Strange property behavior


  • Subject: Re: Strange property behavior
  • From: dreamcat7 <email@hidden>
  • Date: Mon, 15 Sep 2008 19:48:33 +0100


Hmm, Here is the assembly i think we can take a guess why


NSString *n = [[o someMethod] someProperty];

0x00005d42  <+0550>  mov    -0x28(ëp),íx
0x00005d45  <+0553>  lea    0x56a8(ëx),êx
0x00005d4b  <+0559>  mov    (êx),êx
0x00005d4d  <+0561>  mov    êx,0x4(%esp)
0x00005d51  <+0565>  mov    íx,(%esp)
0x00005d54  <+0568>  call   0xc059 <dyld_stub_objc_msgSend>
0x00005d59  <+0573>  mov    êx,íx
0x00005d5b  <+0575>  lea    0x56a4(ëx),êx
0x00005d61  <+0581>  mov    (êx),êx
0x00005d63  <+0583>  mov    êx,0x4(%esp)
0x00005d67  <+0587>  mov    íx,(%esp)
0x00005d6a  <+0590>  call   0xc059 <dyld_stub_objc_msgSend>
0x00005d6f  <+0595>  mov    êx,-0x1c(ëp)


NSString *n = [o someMethod].someProperty;

0x00005d2e  <+0550>  mov    -0x28(ëp),%esi
0x00005d31  <+0553>  lea    0x56bc(ëx),êx
0x00005d37  <+0559>  mov    (êx),êx
0x00005d39  <+0561>  mov    êx,0x4(%esp)
0x00005d3d  <+0565>  mov    %esi,(%esp)
0x00005d40  <+0568>  call   0xc059 <dyld_stub_objc_msgSend>
0x00005d45  <+0573>  lea    0x56b8(ëx),êx
0x00005d4b  <+0579>  mov    (êx),íi
0x00005d4d  <+0581>  lea    0x56bc(ëx),êx
0x00005d53  <+0587>  mov    (êx),êx
0x00005d55  <+0589>  mov    êx,0x4(%esp)
0x00005d59  <+0593>  mov    %esi,(%esp)
0x00005d5c  <+0596>  call   0xc059 <dyld_stub_objc_msgSend>
0x00005d61  <+0601>  mov    íi,0x4(%esp)
0x00005d65  <+0605>  mov    êx,(%esp)
0x00005d68  <+0608>  call   0xc059 <dyld_stub_objc_msgSend>
0x00005d6d  <+0613>  mov    êx,-0x1c(ëp)


In the first example the compiler comes across two left bracket "[[" first of all to say "i have to make room for (possible 2) returned object on the stack". "And if i have room in my registers ill put in the registers." Then when the compiler reads later in its buffer it sees the rightmost bracked ] and has pushed out 2 to find the result for the LHS assignment '=' operator. Just as we would with any 'c' statement, the ; means start again (for the next line of code). But the compiler had to expect us to put two left [[ brackts first to know without reading ahead the whole statement. This is because our traditional obj-c syntax allow allows within limits a pretty much unlimited recursion ( any number of nested statement ) but the trade- off is it must know how much to recurse (because likely it is pushing a bunch of instruction code in the address space).



In second example the new dot operator comes after one fully closed bracket [].
Perhaps the compiler has seen the dot but it was not prepared beforehand and there (may be) an arbritrary number of dot afterward which (may be a struct or other property). So instead of move the memory it calls twice to put the (self) then the (string) into the same temporary space on the buffer. Perhaps it is an effeciency tradeoff for the compiler and the runtime, perhaps it is an undocumented side-effect of property dot (.) syntax and something can be done by apple support.


Here is the relevant page of the apple documentation and it is not mentioned there (and apple gives a similar-looking example). However in the apple example ALL of the methods called are kvc-compliant accessor functions. Wheras in your example they are one property and one accessor method.

I tried changing the method into an accessor by adding the following line of code within the @interface declaration
@property (readonly) MyTestClass* someMethod;


result: Same.
The accessor method (as opposed to regular method) is called twice also. We might speculate that this did not occur to apple because accessor methods can be called any number of times ?



On 15 Sep 2008, at 18:22, atebits wrote:

Yes,

at this line:
NSString *n = [o someMethod].someProperty;

the -someMethod method body is literally executed twice (control isn't
handed back to the calling function until after the second "return
self;".

- (MyTestClass *)someMethod
{
      NSLog(@"someMethod called");
      return self;
}

Loren

On Mon, Sep 15, 2008 at 10:13 AM, Kyle Sluder
<email@hidden> wrote:
On Mon, Sep 15, 2008 at 12:37 PM, Loren Brichter
<email@hidden> wrote:
Call me crazy, but I'm only calling [o someMethod] once in the above
code... so why is it getting called twice?  Imagine my frustration
tracking down the bug when -someMethod has side effects :).

Did you step through it in gdb to see what was going on?

--Kyle Sluder

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


References: 
 >Strange property behavior (From: "Loren Brichter" <email@hidden>)
 >Re: Strange property behavior (From: atebits <email@hidden>)

  • Prev by Date: Re: NSUInteger in for-loop?
  • Next by Date: Re: NSUInteger in for-loop?
  • Previous by thread: Re: Strange property behavior
  • Next by thread: Re: Strange property behavior
  • Index(es):
    • Date
    • Thread