0

I am currently trying to wrap an std::ostringstream into and std::unique_ptr. My current code compiles, but at runtime, I am getting an segmentation fault.

When I do not wrap it - using an old c-style pointer, it runs fine.

For a more detailed overview, I am downloading data using curlpp into an ostringstream.

This is what I am doing:

std::unique_ptr< std::ostringstream > data_stream;
curlpp::Cleanup myCleanup;
*data_stream << curlpp::options::Url(this->m_ressource_url);

The segmentation fault occurs at the last line, this is the backtrace:

0x00007ffff790e2ce in std::ostream::sentry::sentry(std::ostream&) () from /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/libstdc++.so.6
(gdb) bt
#0  0x00007ffff790e2ce in std::ostream::sentry::sentry(std::ostream&) () from /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/libstdc++.so.6
#1  0x00007ffff790e3f7 in std::ostream::write(char const*, long) () from /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/libstdc++.so.6
#2  0x0000000000429be6 in curlpp::internal::Callbacks::StreamWriteCallback (buffer=0x660d1b "Das ist (k)ein Test.", size=1, nitems=20, stream=0x0)
    at OptionSetter.cpp:55
#3  0x00007ffff7b90678 in ?? () from /usr/lib64/libcurl.so.4
#4  0x00007ffff7ba4d38 in ?? () from /usr/lib64/libcurl.so.4
#5  0x00007ffff7bac967 in ?? () from /usr/lib64/libcurl.so.4
#6  0x00007ffff7bad5e5 in curl_multi_perform () from /usr/lib64/libcurl.so.4
#7  0x00007ffff7ba5dd6 in curl_easy_perform () from /usr/lib64/libcurl.so.4
#8  0x0000000000425a78 in curlpp::internal::CurlHandle::perform (this=0x6690b0) at CurlHandle.cpp:52
#9  0x0000000000424fca in curlpp::Easy::perform (this=0x7fffffffd5c0) at Easy.cpp:48
#10 0x00000000004252ba in operator<< (stream=..., request=...) at Easy.cpp:116
#11 0x0000000000424d7f in operator<< (stream=..., url=...) at Options.cpp:34
#12 0x0000000000420bb1 in Model<std::string>::m_download (this=0x7fffffffd878)
    at /home/bueddl/Developement/Studium/Semester 4/SWT/Source/model/src/Model.hpp:98
#13 0x0000000000420a4e in Model<std::string>::refresh (this=0x7fffffffd878)
    at /home/bueddl/Developement/Studium/Semester 4/SWT/Source/model/src/Model.hpp:71
#14 0x00000000004206a5 in main () at /home/bueddl/Developement/Studium/Semester 4/SWT/Source/model/src/main.cpp:56

FYI, my files are from #14 to #12, the code above is a part of the file #12.

Now, this is strange, when I am writing the fallowing code, it works without problems:

std::ostringstream *data_stream = new std::ostringstream();
curlpp::Cleanup myCleanup;
*data_stream << curlpp::options::Url(this->m_ressource_url);

Both cases are passing pointers to the operator<<, but I seem to be wrong.

Where am I wrong?

Thanks for your help :)

Note: I wanted to use the unique_ptr for implementing sink-source pattern.

  • 3
    Uh... `data_stream` holds a `NULL` pointer. Just creating an instance of `unique_ptr` doesn't by itself create an instance of `T` for it to hold. – Igor Tandetnik Jul 18 '14 at 22:59

1 Answers1

3

unique_ptr doesn't automatically create an instance of the pointed to object. You still need to do that yourself. So your code is trying to use a NULL pointer to a stream.

You need to do something like:

std::unique_ptr< std::ostringstream > data_stream(new std::ostringstream);
curlpp::Cleanup myCleanup;
*data_stream << curlpp::options::Url(this->m_ressource_url);
Casey
  • 41,449
  • 7
  • 95
  • 125
TheUndeadFish
  • 8,058
  • 1
  • 23
  • 17