title: Can I detach the buffer from a std::string in C++?
body: <p>I'm new to C++. I was assuming <code>std::string</code> uses a reference count to determine when to release the buffer. In the following example, <code>s</code> buffer will be released when <code>f()</code> returns. What if I wanted to give ownership of the string buffer to <code>give_ownership_of</code> and not to release it?</p>
  
  <pre><code>void f() 
  { 
      string s = read_str();  
      give_ownership_of(s); 
  }
  </code></pre>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Let me add more details into the question. The actual code looks like this,</p>
  
  <pre><code>string read_str();
  
  void write_str_async(const char *str, void (*free_fn)(const char*));
  
  void f() {
    string s = read_str();
    // write_str_async() need to access the buffer of s after f() returns.
    // So I'm responsible to keep s alive until write_str_async() calls free_fn to release the buffer when the write is done.
    // The PROBLEM here is that s will be released when the variable scope ends. NOTE: I'm not able to change either read_str() or write_str_async() here.
    write_str_async(s.c_str(), my_free_fn);
  }
  </code></pre>