未处理的异常/访问冲突,只要我在变量上使用 OutputDebugStringW()
Unhandled exception / Access violation as soon as I use OutputDebugStringW() on a variable
我正在编写一个程序,该程序拆分为线程,每个线程都经过定时,然后将时间加在一起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_time
,LeaveCriticalSection
s,然后结束。
我尝试在OutputDebugStringW()
周围添加EnterCriticalSection
和LeaveCriticalSection
,但这无助于修复错误。
有什么想法吗?
更新 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);
相关文章:
- 如何创建一个CMake变量,除非显式重写,否则使用默认值
- 将成员变量添加到共享库中的类中,不会破坏二进制兼容性吗
- 将数组的地址分配给变量并删除
- 为"adjacent"变量赋值时出现问题
- enum是C++中的宏变量还是整数变量
- 在全局变量中保存类的实例以重新创建类(创建"backup")
- 用C++中的一个变量定义一个常量
- 具有奇怪重复模板模式的派生类中的成员变量已损坏
- 你能重载对象变量名本身返回的内容吗
- 内置函数可查看CPP中的成员变量
- 是否可以初始化不可复制类型的成员变量(或基类)
- 尝试通过多个向量访问变量时,向量下标超出范围
- 试图让变量检查数组中的某些内容
- Cpp-Tuple使用带有变量的get
- 将包含C样式数组的对象初始化为成员变量(C++)
- 当vector是tje全局变量时,c++中vector的内存管理
- 通过多个头文件使用常量变量
- std::threads可以从Windows DLL中的全局变量创建/销毁吗?
- 执行函数时导致崩溃的变量
- 变量没有改变?通过向量的函数调用