Hi All,

In below code snippet i am getting segmentation fault. Could you let me know what can be cause.
usually we will face segmentation fault if auto_ptr is used due to ownershpt issue (Assignee pointer becomes NULL)
but why i am facing the same issue with unique_ptr move semantics.

#include<iostream>

#include<memory>

using namespace std;

class Test
{
public:
 Test(int a = 0 ) : m_a(a)
 {
 }
 ~Test( )
 {
  cout<<"Calling destructor"<<endl;
 }
public:
 int m_a;
};


//***************************************************************
void Fun(std::unique_ptr<Test> p1 )
{
 cout<<p1->m_a<<endl;
}
//***************************************************************
int  main( )
{
 std::unique_ptr<Test> p( new Test(5) );
 Fun(std::move(p));
 cout<<p->m_a<<endl;

return 1;
}

p does not own anything, that is why it causes a crash here. How to resolve this issue.

Recommended Answers

All 5 Replies

Surely this is exactly what's expected.

You create a unique pointer. You then construct another unique_ptr from that one, using the move constructor. So the original gets set to null. That's what happens when you move a unique_ptr. It's the whole point of a unique_ptr.

Then you try to use that null pointer (line 32), and you get a segFault.

As Moschops said you are moving the data from p so trying to use it after that results in your segfault. std::unique_ptr doe have an operator bool() that will return true if get() != nullptr. Using that we can protect against using the pointer if it no longer manages a pointer.

#include<iostream>
#include<memory>

using namespace std;

class Test
{
public:
    Test(int a = 0) : m_a(a)
    {
    }
    ~Test()
    {
        cout << "Calling destructor" << endl;
    }
public:
    int m_a;
};


//***************************************************************
void Fun(std::unique_ptr<Test> p1)
{
    cout << p1->m_a << endl;
}
//***************************************************************
int  main()
{
    std::unique_ptr<Test> p(new Test(5));
    Fun(std::move(p));
    if (p)
        cout << p->m_a << endl;
    else
        cout << "p is empty";

    return 1;
}

Live Example: http://coliru.stacked-crooked.com/a/398497798ee1825f

As a rule of thumb if you move() something you should no longer use it.

If the argument to Fun ()is a reference and not a copy, then that should also work.

Thanks ruberman, i tried below code and found it is functional now and expecting the resolution of this problem in similar way . My question was related with comparision with auto_ptr and unique_ptr . if we perform same (passing object as value) with auto_ptr we will get crash because auto_ptr will set the assignee pointer to null . If both auto_ptr and unique_ptr perform both things similarly then what is the significance of move semantics. Could you explain

void Fun( const std::unique_ptr<Test>& p1 )
{
 cout<<p1->ma;
}

what i understood auto_ptr use copy constructor and unique_ptr use move function to transfer the ownership. But in both cases assignee pointer becomes null then what is the significance of move semantics here. I beleive compiler should flash the error if unique_ptr is passed as a value like below statement.
could you throw some light on this.

unique_ptr<Test> a = p;//deleted function (copy constructor)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.