Is c_str () from rvalue unsafe?

Can anyone explain why the following crashes with access violation in 0xFFFFFFFFon avformat_open_input?

std::string u8(const std::wstring& str)
{
    return boost::locale::conv::utf_to_utf<char>(str);
}

AVFormatContext* open_input(const std::wstring& filename)
{
    AVFormatContext* context = nullptr;
    avformat_open_input(&context, u8(filename).c_str(), nullptr, nullptr);
    avformat_find_stream_info(context, nullptr);
    return context;
}

while the following works:

AVFormatContext* open_input(const std::wstring& filename)
{
    auto u8filename = u8(filename);
    AVFormatContext* context = nullptr;
    avformat_open_input(&context, u8filename.c_str(), nullptr, nullptr);        
    avformat_find_stream_info(context, nullptr);
    return context;
}
+3
source share
1 answer

The result u8(filename).c_str()should be used until avformat_open_inputit returns. It will probably keep the pointer you give it, and then use it in time avformat_find_stream_info.

Publish documents for these avformat functions or their implementation so that we can see if this is actually done.


, avformat_open_input - . , undefined - . , valgrind , , - .

+1

All Articles