[LLVMbugs] [Bug 8623] New: destructor not called on temporary in ternary

bugzilla-daemon at llvm.org bugzilla-daemon at llvm.org
Mon Nov 15 22:50:43 PST 2010


http://llvm.org/bugs/show_bug.cgi?id=8623

           Summary: destructor not called on temporary in ternary
           Product: clang
           Version: trunk
          Platform: PC
        OS/Version: Linux
            Status: NEW
          Severity: normal
          Priority: P
         Component: C++
        AssignedTo: unassignedclangbugs at nondot.org
        ReportedBy: nlewycky at google.com
                CC: llvmbugs at cs.uiuc.edu, dgregor at apple.com


An object constructed in one side of a ?: expression doesn't get its
constructor called. Here's an example program demonstrating the problem:

  #include <cstdio>
  using namespace std;

  struct X {
    X() { printf("X() : %x\n", this); }
    ~X() { printf("~X() : %x\n", this); }
    X(const X &other) { printf("X(const X& %x) : %x\n", &other, this); }
  };

  struct Y {
    Y() { printf("Y() : %x\n", this); }
    ~Y() { printf("~Y() : %x\n", this); }
    Y(const Y &other) { printf("Y(const Y& %x) : %x\n", &other, this); }

    X ToString() const { return X(); }
  };

  int main(int argc, char** argv) {
    Y y;
    0 ? X() : y.ToString();
  }

which emits:

  $ clang++ -O0 -g ternary-leak.cc -o ternary-leak -Wno-format
  $ ./ternary-leak 
  Y() : 96ceb008
  X() : 96ceb000
  ~Y() : 96ceb008

I don't think that's permitted. By contrast, g++ shows two calls to ~X() and
indeed if I just replace the line in main with "y.ToString();" then ~X() is
called.

-- 
Configure bugmail: http://llvm.org/bugs/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are on the CC list for the bug.



More information about the llvm-bugs mailing list