1

I keep getting a debug error thrown, telling me that abort() has been called and then when I go to debug in Visual Studio it takes me to the following code (the last line is where it throws):

void __cdecl _NMSG_WRITE (
        int rterrnum
        )
{
        const wchar_t * const error_text = _GET_RTERRMSG(rterrnum);

        if (error_text)
        {
            int msgshown = 0;
#ifdef _DEBUG
            /*
             * Report error.
             *
             * If _CRT_ERROR has _CRTDBG_REPORT_WNDW on, and user chooses
             * "Retry", call the debugger.
             *
             * Otherwise, continue execution.
             *
             */

            if (rterrnum != _RT_CRNL && rterrnum != _RT_BANNER && rterrnum != _RT_CRT_NOTINIT)
            {
                switch (_CrtDbgReportW(_CRT_ERROR, NULL, 0, NULL, L"%s", error_text))
                {
                case 1: _CrtDbgBreak(); msgshown = 1; break;

It appears when I step over whatever-is-the-last-line in the below function:

X::my_func(){

    //a,b,c are 2x ints and a unordered_map
    std::thread t1(&X::multi_thread_func, this, a, b, c);

    int c = 0;

    //The debug error message doesn't appear until I step over this line. If I were
    //to add further code to this function then the error only appears after stepping
    //over the last line in the function.
    int c1 = 0;
}

I appreciate there's not a lot to go on- but could people give me tips how I could continue my investigation from within Visual Studio 2012?

EDIT: If I remove the multithreaded call I don't get the error

1 Answer 1

1

A std::thread instance must either be joined or detached, before it can be destructed (what happens, as soon as t1 goes out of scope). Otherwise, the destructor of std::thread will call std::terminate(). This might be the cause of that abort.

X::my_func(){

    //a,b,c are 2x ints and a unordered_map
    std::thread t1(&X::multi_thread_func, this, a, b, c);
    t1.join();// <--- ... however, using a thread like this makes little to no sense.

    int c = 0;

    //The debug error message doesn't appear until I step over this line. If I were
    //to add further code to this function then the error only appears after stepping
    //over the last line in the function.
    int c1 = 0;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Hi Sam. The reason I don't join the thread in X::my_func() is because the function which called X::my_func() then calls X::my_func() again to create another thread (but passing in different parameters for the second thread to operate on).
In this case, you may return that thread instance, so that all created threads can be joined at some point.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.