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.