As I said, either ARC or C++ objects with constructors and destructors requires 
compiler to insert code into the beginning and ending of the current scope (For 
C++, it is calls to constructors and destructors; for Objective-C ARC, it is 
ARC release/autorelease function calls.). As a rule of thumb, scope is defined 
by braces.

To explain this, here is an example:

- (void)method
{ // Begin of scope. Here I used a Objective-C method implementation, but 
anything that involves a set of braces count.
    NSString *objcString = CFBridgingRelease(CFSTR(“string”)); // Guarantee a 
new object, ARC managed.
    std::string cxxString = “string”;

    // …

    // At the end of scope, as like the end of this method, code like following 
will be inserted:
    // cxxString.dtor(); // There is no formal way to call C++ destructor, but 
you get the idea.
    // objc_release(objcString); // This is indeed what the compiler inserts. 
Just check the assembler from clang -S
} // End of scope.

But this gets confusing when switch statement is involved. The beginning of the 
braces certainly is not the start point of execution, the end of braces may not 
be the end of execution as well, and if you count the declare-before-use rule 
of C, the thing becomes extremely difficult to follow.

For Java, they require every case (or set of cases) in switch statement be 
terminated with a break statement, essentially implying a set of braces between 
case and break, the situation is better to understand. However with falling 
through of C, it is impossible to imply scope. Hence, C++ compiler emits a 
warning on that (they don’t leak objects, just at most some uninitialised 
objects, since all objects are stack-based) and Objective-C ARC just flat out 
rejecting this (since that leaks memory.)

By introducing a set of braces there, it makes another scope so that ARC (or 
C++) can insert the code appropriately. Also for some memory optimisations, 
this new scope can be a good place to introduce a new stack frame for locals.

On Oct 23, 2013, at 0:16, Scott Ribe <[email protected]> wrote:

> On Oct 22, 2013, at 9:50 AM, Sean McBride <[email protected]> wrote:
> 
>> I don't think you can quote the Standard about 'goto' and just wave your 
>> hands and say it applies to 'switch' also.  :)  The Standard's description 
>> of 'switch' should contain the answer
> 
> Switch is just a pile of goto, and I'm pretty sure the standard says so 
> somewhere.
> 
>> However, it does seem to me there is a clang bug here somewhere.
> 
> I don't think so. It's perhaps worth noting that in C++ or Objective-C++ you 
> get a similar warning when you do this with an instance that has a 
> constructor or destructor, and this goes back to versions of GCC from at 
> least several years ago--trust me, I know about that ;-)
> 
> -- 
> Scott Ribe
> [email protected]
> http://www.elevated-dev.com/
> (303) 722-0567 voice
> 
> 
> 
> 
> 
> _______________________________________________
> 
> 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:
> https://lists.apple.com/mailman/options/cocoa-dev/xcvista%40me.com
> 
> This email sent to [email protected]

Attachment: signature.asc
Description: Message signed with OpenPGP using GPGMail

_______________________________________________

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

This email sent to [email protected]

Reply via email to