Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Objective-C parameter mutation



On Oct 15, 2008, at 7:31 PM, James Trankelson wrote:

I have a question about method parameters in Objective C. A long time
ago, I had a method like the following:

-(void) foo:(float)val { }

... and when I would call it ([inst foo:0.001], for example), the
value inside the foo method would NOT be 0.001. At the time, I
believed there to be an Objective C requirement that parameters to
methods need to be pointers to objects, not primitive values.

Nope, no such requirement.


So, I've got the following method:

+ (void)done:(MyObj*)dObj structure:(void*)pStruct

... and when I call it with a "structure" parameter, say a malloc'ed
char* at address 0x171eb070, as soon as I step into this method, the
address is no longer 0x171eb070, but some garbage value that causes
all sorts of trouble. Here's to hoping someone can tell me why this
doesn't work...

Do a clean build. Pay attention to and address _all_ warnings.

In particular, the compiler may warn about sending messages that haven't been declared. It may assume parameter types that aren't the same as the real method, which can cause garbage to be received.

Also, be careful when stepping in the debugger. If you step by instruction, you may need to step a few instructions into a function/ method implementation before the stack frame is set up so that the debugger can really interpret the values of arguments. In other words, for the first few instructions it may show you garbage-seeming values for arguments and locals.

Cheers,
Ken

_______________________________________________

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:
http://lists.apple.com/mailman/options/cocoa-dev/email@hidden

This email sent to email@hidden
References: 
 >Objective-C parameter mutation (From: "James Trankelson" <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.