未处理的异常/访问冲突,只要我在变量上使用 OutputDebugStringW()

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

本文关键字:变量 OutputDebugStringW 异常 访问冲突 未处理      更新时间:2023-10-16

我正在编写一个程序,该程序拆分为线程,每个线程都经过定时,然后将时间加在一起total_time

我使用互斥锁保护total_time

该程序运行良好,直到我添加了"OutputDebugStringW",这是我开始收到这些未处理的异常/访问冲突错误的时候。

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));

在每个线程中,它都会进行一些计算,它乘以EntersCriticalSection,将所花费的时间添加到total_timeLeaveCriticalSection s,然后结束。

我尝试在OutputDebugStringW()周围添加EnterCriticalSectionLeaveCriticalSection,但这无助于修复错误。

有什么想法吗?

更新 1:

这是 MandelbrotThread 函数 -

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, 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是要渲染的集合的边(因此每个线程都相同),要开始的行 (y_start) 和要渲染的行数。

reinterpret_cast一个

数字变成一个字符串肯定会关闭编译器,但不会让你的程序神奇地工作。您需要使用 sprintf 或最好是 boost::lexical_cast 转换它(尽管我猜后者不适合您)。

WCHAR buf[32];
wsprintf(buf, L"%I64dn", total_time);
OutputDebugStringW(buf);