Search code examples
c++boostsplit

Program crash when using boost::iter_split?


my main function in vs2022-v143 and boost-v1.79:

 struct func_info
    {
        func_info(const std::wstring& _var, const std::wstring& _name) :var(_var), func_name(_name) { }
        std::wstring var;
        std::wstring func_name;
        std::vector<std::wstring> values;
    };

void main_function(){
        for (unsigned i = 0; i < 1; ++i)
        {
            func_info _func{ L"zz1",std::wstring{}};
            get_split(L"abcxyz(c2,c3)", _func.values);
        } //  <-- program crash 


   }
   //myfunc.h
   void get_split(const wstring& input, std::vector<std::wstring>& result){
    ...
        boost::iter_split(result, input, boost::algorithm::first_finder(L","));
   }

I got the expected result but my problem is program crashes when exiting or next the loop

enter image description here

is there a problem with _func.values? I don't know where the cause is.

_CONSTEXPR20 void _Container_base12::_Orphan_all_unlocked_v3() noexcept {
    if (!_Myproxy) { // no proxy, already done
        return;
    }

    // proxy allocated, drain it
    for (auto& _Pnext = _Myproxy->_Myfirstiter; _Pnext; _Pnext = _Pnext->_Mynextiter) { // TRANSITION, VSO-1269037
        _Pnext->_Myproxy = nullptr;
    }
    _Myproxy->_Myfirstiter = nullptr;
}

Solution

  • You have Undefined Behavior elsewhere. The problem is not in the code shown (modulo obvious typos):

    Live On MSVC as well as GCC with ASAN:

    #include <boost/algorithm/string.hpp>
    #include <iomanip>
    #include <iostream>
    #include <string>
    #include <vector>
    
    // myfunc.h
    void get_split(const std::wstring& input, std::vector<std::wstring>& result) {
        // ...
        boost::iter_split(result, input, boost::algorithm::first_finder(L","));
    }
    
    struct func_info {
        func_info(std::wstring _var, std::wstring _name)
            : variable(std::move(_var)), func_name(std::move(_name)) {}
        std::wstring variable;
        std::wstring func_name;
        std::vector<std::wstring> values;
    };
    
    int main() {
        for (unsigned i = 0; i < 10; ++i) {
            func_info _func{L"zz1", {}};
            get_split(L"abcxyz(c2,c3)", _func.values);
    
            for (auto& s : _func.values) {
                std::wcout << L" " << std::quoted(s);
            }
            std::wcout << L"\n";
        }
    }
    

    Prints

     "abcxyz(c2" "c3)"
     "abcxyz(c2" "c3)"
     "abcxyz(c2" "c3)"
     "abcxyz(c2" "c3)"
     "abcxyz(c2" "c3)"
     "abcxyz(c2" "c3)"
     "abcxyz(c2" "c3)"
     "abcxyz(c2" "c3)"
     "abcxyz(c2" "c3)"
     "abcxyz(c2" "c3)"