title: How to get shared_ptr from raw pointer?
body: <p>I'm doing c/c++ mix programming, like this:<br>
  common.h</p>
  
  <pre><code>#ifdef __cplusplus
  typedef opentracing::Span CTraceSpan;
  #else
  struct CTraceSpan;
  typedef struct CTraceSpan CTraceSpan;
  #endif
  
  extern "C" CTraceSpan* c_StartServerSpan();
  extern "C" CTraceSpan* c_FinishServerSpan(CTraceSpan *span);
  </code></pre>
  
  <p>trace2c.cpp  </p>
  
  <pre><code>extern "C" CTraceSpan* c_StartServerSpan(){
  auto server_span = hwtrace::StartServerSpan();
  return server_span.get();
  }  
  
  extern "C" CTraceSpan* c_FinishServerSpan(CTraceSpan *span){
  std::shared_ptr&lt;opentracing::Span&gt; s_span(std::move(span));
  
  auto server_span = hwtrace::FinishServerSpan(s_span);
  return server_span.get();
  }
  </code></pre>
  
  <p>and in trace.c</p>
  
  <pre><code>CTraceSpan *span = NULL;
  span = c_StartClientSpan();
  c_FinishClientSpan(span);
  </code></pre>
  
  <p>when I run my project, I got an error:  </p>
  
  <p>Segmentation fault (core dumped)</p>
  
  <p>the question below, said that can't create shared_ptr from an raw pointer, but I need to transmit a pointer form c to c++, what should i do?</p>
  
  <p><a href="https://stackoverflow.com/questions/28670842/segmentation-fault-when-using-a-shared-ptr">Segmentation fault when using a shared_ptr</a>  </p>
  
  <blockquote>
    <p>You should never create shared_ptr from an ordinary pointer as you do here:</p>
  
  <pre><code>shared_ptr&lt;ParticleEmitter&gt;(&amp;e)
  </code></pre>
    
    <p>This tries to free the ParticleEmitter twice. Once as the vector holding the ParticleEmitter objects goes out of scope, and once as the shared_ptr goes out of scope.</p>
  </blockquote>