开发者

Unhandled exception / Access violation as soon as I use OutputDebugStringW() on a variable

开发者 https://www.devze.com 2023-03-06 11:34 出处:网络
I\'m writing a program which splits off into threads, each thread is timed, and then the times are added together in total_time.

I'm writing a program which splits off into threads, each thread is timed, and then the times are added together in total_time.

I protect total_time using a mutex.

The program was working fine, until I added 'OutputDebugStringW', which is when I started getting these Unhandled exception / Access violation errors.

for (int loop = 0; loop < THREADS; loop++)
{
    threads[loop] = (HANDLE) _beginthreadex(NULL, 0, MandelbrotThread, &m_args[loop], 0, NULL);
}
WaitForMultipleObjects(THREADS, threads, TRUE, INFINITE);
OutputDebugStringW(LPCWSTR(total_time));

Within each of these threads, it does some calculation which it times, EntersCriticalSection, adds the time taken to total_time, LeaveCriticalSections, then ends.

I tried adding EnterCriticalSection and LeaveCriticalSection around OutputDebugStringW() but it didn't help to fix the error.

Any thoughts?

Update 1:

Here is the MandelbrotThread function -

unsigned int __stdcall MandelbrotThread(void *data)
{
    long long int time = get_time();
    MandelbrotArgs *m_args = (MandelbrotArgs *) data;
    compute_mandelbrot(m_args->left, m_args->right, m_args->top, m_args->bottom,开发者_JS百科 m_args->y_start, m_args->lines_to_render); 
    time = time - get_time(); 
    EnterCriticalSection(&time_mutex);
    total_time = total_time + time; 
    LeaveCriticalSection(&time_mutex);
    return 0; 
}

m_args are the sides of the set to be rendered (so the same for every thread), and the line to start on (y_start) and the number of lines to render.


reinterpret_cast'ing a number to a string will certainly shut up the compiler, but will not make your program magically work. You need to convert it, using sprintf, or preferrably boost::lexical_cast (although I'm guessing the latter's not an option for you).

WCHAR buf[32];
wsprintf(buf, L"%I64d\n", total_time);
OutputDebugStringW(buf);
0

精彩评论

暂无评论...
验证码 换一张
取 消