将rvalue指定为返回值时出现分段错误

时间:2015-04-15 20:44:09

标签: c++ c++11 gcc clang rvalue-reference

我有一个构造新对象的工厂类。 构造函数和移动赋值运算符。

我认为,为了帮助传达对象必须移动到位而不是复制的想法,我会返回一个右值引用。但是,执行此操作时,似乎编译器始终生成的代码会破坏正在返回的“过期”对象,然后将相同(已销毁!)对象提供给移动构造函数或移动赋值运算符。

所以我想知道;我违反了标准中的某些内容吗?如果是这样,是否有警告(错误,最好)我可以启用以防止我继续做一些如此愚蠢的事情?或者,如果我没有违反任何标准,那么我认为这是一个编译器错误?

// g++ (Ubuntu 4.9.2-0ubuntu1~14.04) 4.9.2
// g++ test.cpp -std=c++11

#include <iostream>
#include <memory>

struct PTR {
    char * blah = nullptr;

    PTR(char* blah) : blah(blah)
    {
        std::cout << "\tctor  @" << (void*)this << std::endl;
    }

    PTR(const PTR & copy_ctor) : blah(new char)
    {
        *blah = *copy_ctor.blah;
        std::cout << "\tcopy @@" << (void*)this << "\t<-" << (void*)&copy_ctor << std::endl;
    }

    PTR(PTR && move_ctor) : blah(move_ctor.blah)
    {
        move_ctor.blah = nullptr;
        std::cout << "\tctor&&@" << (void*)this << "\t<@" << (void*)&move_ctor << std::endl;
    }

    PTR & operator=(const PTR & copy_assign)
    { delete blah;
        blah = new char;
        *blah = *copy_assign.blah;
        std::cout << "copyas@" << (void*)this << "\t<-" << (void*)&copy_assign << std::endl;
        return *this;
    }

    PTR & operator=(PTR && move_assign)
    {
        delete blah;
        blah = move_assign.blah;
        move_assign.blah = nullptr;
        std::cout << "\tmove&&@" << (void*)this << "\t<@" << (void*)&move_assign << std::endl;
        return *this;
    }

    ~PTR()
    {
        std::cout << "\tdtor~~@" << (void*)this << std::endl;
        delete blah;
    }
};

PTR make_ptr_l() {
    PTR ptr(new char());
    return std::move(ptr); // Without std::move, compiler *may* opt to copy the class, which is undesired
}

PTR && make_ptr_r() {
    PTR ptr(new char());
    return std::move(ptr); // Requires std::move to turn ptr into rvalue, otherwise compiler error
}

int main() {
    std::cout << "lvalue: \n" << std::flush;
    PTR ptr = make_ptr_l();
    std::cout << "successful\nrvalue new: \n" << std::flush;
    {
        PTR ptr_r = make_ptr_r();
        std::cout << "successful\nrvalue assign: \n" << std::flush;
    }
    ptr = make_ptr_r();
    std::cout << "successful" << std::endl;
    return 0;
}

使用上面的代码,您可以看到以下输出:

lvalue: 
    ctor  @0x7ffed71b7a00
    ctor&&@0x7ffed71b7a30   <@0x7ffed71b7a00
    dtor~~@0x7ffed71b7a00
successful
rvalue new: 
    ctor  @0x7ffed71b7a00
    dtor~~@0x7ffed71b7a00
    ctor&&@0x7ffed71b7a40   <@0x7ffed71b7a00
successful
rvalue assign: 
    dtor~~@0x7ffed71b7a40
*** Error in `./a.out': double free or corruption (fasttop): 0x0000000001d1bc40 ***
Aborted (core dumped)

正如您在rvalue new之后看到的那样,构造一个对象,然后立即销毁,然后将被销毁的对象传递给移动构造函数。因为移动构造函数因此访问被破坏的变量,所以这是分段错误的来源。

我已尝试使用g++ (Ubuntu 4.9.2-0ubuntu1~14.04) 4.9.2以及Apple LLVM version 6.0 (clang-600.0.57) (based on LLVM 3.5svn)

1 个答案:

答案 0 :(得分:2)

您将返回对临时的引用。您可以从输出中说明这一点:

rvalue new: 
    ctor  @0x7ffed71b7a00
    dtor~~@0x7ffed71b7a00
    ctor&&@0x7ffed71b7a40   <@0x7ffed71b7a00

在移动构造7a00之前构建并销毁7a40 。您恰好返回右值引用而不是左值引用这一事实无关紧要。它基本上仍然是这样的形式:

T& foo() {
    T object;
    return object;
}

这就是make_ptr_l有效的原因 - 你要返回一个值,而不是一个引用。并且std::move()没有必要。