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


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

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


           NSString *n = [o someMethod].someProperty;

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


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 PROTECTED]> wrote:
On Mon, Sep 15, 2008 at 12:37 PM, Loren Brichter
<[EMAIL PROTECTED]> 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 protected])

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:
http://lists.apple.com/mailman/options/cocoa-dev/dreamcat7%40googlemail.com

This email sent to [EMAIL PROTECTED]

_______________________________________________

Cocoa-dev mailing list ([email protected])

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to