线程创建时内存注意释放
2014-08-18 11:24
176 查看
情况:近来学习之前的老代码,发现 申请内存 ,然后指针传给线程的回调函数中, 但是一直都没有找到释放的地方。。 所以写下面小demo 学习下。。
#include "stdafx.h" #include<iostream> #include <atlstr.h> #include <iostream> using namespace std; DWORD WINAPI testThreadProc(LPVOID lpParameter) { if (NULL != lpParameter) { delete [] lpParameter; // 若是注释掉这一句,在任务管理器中会看到内存在不断的上升。。 lpParameter = NULL; } cout << " test thread End.." << endl; return 0; } int _tmain(int argc, _TCHAR* argv[]) { DWORD dwThreadId; for (int i = 0; i < 20; ++i) { int* pInt = new int[1000 * 10000]; if (NULL == pInt) { cout << "Allocate Memory Failed.. " << endl; break; } HANDLE hThread = CreateThread(NULL, 1024 * 1024 * 20, testThreadProc, pInt, 0, &dwThreadId); if (NULL == hThread) { cout << "Create Thread Failed..." << endl; // 注意线程创建失败也把内存给释放掉 if (NULL != pInt) { delete [] pInt; pInt = NULL; } } else { cout << "Create Thread Successed..." << endl; } Sleep(1000); // 便于观察 } return 0; }
相关文章推荐
- 下列哪些语句关于内存回收的说明是正确的? (b ) A、 程序员必须创建一个线程来释放内存
- linux创建线程时,需注意线程内存回收问题!
- 创建非模态对话框释放内存的问题
- RETURN前注意释放内存,初始化。。。
- pthread线程创建注意事项
- Linux C 循环创建线程问题!!300 个线程 内存不足!!
- C++ vector容器释放内存应注意的地方(三)
- 跟踪由new创建的对象的所未释放的内存资源的方法
- mysql 临时表和内存表创建 查询 删除以及注意事项
- Pthread创建线程后必须使用join或detach释放线程资源
- 关于创建内存DC用于贴图的注意事项
- Windows线程创建、退出及资源释放
- flex中关于内存释放几个注意的地方(zz)
- 如何释放线程中创建的QUdpSocket对象
- 界面线程结束可能导致局部变量内存不能释放!
- 释放内存的时候,一定要注意时机
- flex开发中关于内存释放几个注意的地方
- 在DC操作中SelectObject后要注意释放内存
- C++ vector容器释放内存应注意的地方(一)
- php unset销毁变量释放内存注意事项