sap
sap

Reputation: 1228

Moving a std::shared_ptr crashes the program

I have to build a small OpenGL wrapper for a work. I'm trying to avoid writing copy constructors and copy assignment for all my classes.

The one way to be really lazy and never write copy is to use pointers, but since pointers are evil I'm trying to use exclusively std::shared_ptr.

Problem is by using a constructor that receives a std::shared_ptr by value my program crashes, and when using perfect forwarding it only works when I'm passing an lvalue.

// this class doesn't have any default, copy constructors.
class Dep
{
    Dep(std::string path, GLenum type);
};

class Program
{
std::shared_ptr<Dep> dep1;
std::shared_ptr<Dep> dep2;

(...)

I have tried 2 different kinds of constructors:

template <class T, class = typename std::enable_if<std::is_constructible<std::shared_ptr<Dep>, T>::value>::type>
Program(T&& dep1, T&& dep2)
: dep1(std::forward<T>(dep1)), dep2(std::forward<T>(dep2))
{
}

and the other one

Program(std::shared_ptr<Dep> dep1, std::shared_ptr<Dep> dep2)
: dep1(std::move(dep1)), dep2(std::move(dep2))
{
}

What I want to do is being able to pass pass either an lvalue or rvalue shared pointer, but it doesn't work it crashes every time unless I use lvalue on the forward one.

// passing these work on the std::forward one, but that's the only case it works
// if i try to use std::make_shared as parameter (for rvalue) it crashes on both
// the std::move and std::forward ones.
auto vs = std::make_shared<GLShader>("TriangleVS.glsl", GL_VERTEX_SHADER);
auto fs = std::make_shared<GLShader>("TriangleFS.glsl", GL_FRAGMENT_SHADER);

summary: lvalue on the std::forward one works. rvalue on std::forward does not work. lvalue or rvalue on the std::move one dont work. it just hangs the program when the std::shared_ptr constructor is called (inside the Program constructor).

I watched Scott Mayers universal references talk and I thought I was understanding this, and this happens to me.

Upvotes: 3

Views: 2064

Answers (1)

sehe
sehe

Reputation: 392921

I don't see anything wrong with this code, and it tests out OK on http://ideone.com/jlShgB too:

#include <memory>
#include <utility>
#include <string>
#include <cassert>

enum GLenum { foo };

// this class doesn't have any default, copy constructors.
struct Dep
{
    Dep(std::string path, GLenum type) {}
    Dep() = delete;
    Dep(Dep const&) = delete;
};

struct Program
{
    std::shared_ptr<Dep> dep1;
    std::shared_ptr<Dep> dep2;

#if 1
    template <class T, class = typename std::enable_if<std::is_constructible<std::shared_ptr<Dep>, T>::value>::type>
    Program(T&& dep1, T&& dep2)
        : dep1(std::forward<T>(dep1)), dep2(std::forward<T>(dep2))
    {
    }
#else
    Program(std::shared_ptr<Dep> dep1, std::shared_ptr<Dep> dep2)
        : dep1(std::move(dep1)), dep2(std::move(dep2))
    {
    }
#endif
};

int main()
{
    auto dep1 = std::make_shared<Dep>("dep1", foo);
    auto dep2 = std::make_shared<Dep>("dep2", foo);
    Program p(std::move(dep1), std::move(dep2));

    assert(!dep1 && !dep2);
}

Of course if you change #if 1 to #if 0, the assert will raise an exception because the dep1/dep2 will not have been moved from.

This leads me to suspect another issue somewhere else. If you can isolate a SSCCE that exhibits the problem, please let me know.

Upvotes: 1

Related Questions