Quuxplusone / LLVMBugzillaTest

0 stars 0 forks source link

Obj-C++ instance method that returns a C++ derived class causes crash #9570

Closed Quuxplusone closed 13 years ago

Quuxplusone commented 13 years ago
Bugzilla Link PR10835
Status RESOLVED FIXED
Importance P normal
Reported by Brad Marston (bradmarston@mac.com)
Reported on 2011-09-02 08:40:04 -0700
Last modified on 2011-09-06 18:52:35 -0700
Version unspecified
Hardware Macintosh MacOS X
CC dgregor@apple.com, efriedma@quicinc.com, kremenek@apple.com, llvm-bugs@lists.llvm.org
Fixed by commit(s)
Attachments Demo.zip (34859 bytes, application/zip)
Blocks
Blocked by
See also
Created attachment 7195
Xcode 4 project that demonstrates the problem with minimal code

An Objective-C++ instance method that returns a derived C++ object causes a
crash when the code is compiled with clang but works under LLVM GCC.  The
debugger says that a pointer being freed wasn't allocated.   The problem occurs
under both OS X 10.6 and 10.7, and with versions of clang that support
Objective-C++.

The attached Xcode project illustrates the problem with minimal code.  Note
that the function "D dummy2()" is able to properly return a derived C++ object
of class D, but the Obj-C instance method "- (D) dummy" fails (but only when
compiled with clang).  Obj-C instance properly return C++ objects of the base
class (B).
Quuxplusone commented 13 years ago

Attached Demo.zip (34859 bytes, application/zip): Xcode 4 project that demonstrates the problem with minimal code

Quuxplusone commented 13 years ago

The given testcase has undefined behavior given that it doesn't define a copy constructor for D. However, ideally we shouldn't be calling it (i.e. this is a missed case of NRVO).

Quuxplusone commented 13 years ago

Yes, thanks, when compiled with llvm-gcc, only the assignment operator is invoked; when compiled with clang first the copy constructor (when added to the code) is invoked, and then the assignment operator. It would be a valuable optimization to not call the copy constructor.

Quuxplusone commented 13 years ago

This is also rdar://problem/10050178.

Quuxplusone commented 13 years ago

Fixed in Clang r139175.

Quuxplusone commented 13 years ago
(In reply to comment #4)
> Fixed in Clang r139175.

Excellent!