//这个是自定义消息 收到消息就执行函数,就是创建线程播放音乐,
//这样当多收到几次这个消息后,就有内存漏泄
LRESULT CP01Dlg::OnPlayMusic(WPARAM wParam,LPARAM lParam)
{
DWORD dwThreadID=0;
CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)threadProc,NULL,0,&dwThreadID);
ResumeThread((HANDLE)dwThreadID);
return 0;
}以下这个函数是播放C:\WINDOWS\Media\ding.wav
void threadProc(void)
{
CString miccommand; miccommand.Format(_T("open \"%s\" Alias movie"),filepath);
MCIERROR error=mciSendString(miccommand.GetBuffer(miccommand.GetLength()),NULL,NULL,NULL); //打開文件
TRACE("error===>%d\r\n",error);
miccommand.ReleaseBuffer();
if(error)
{
return;
}
error=mciSendString(_T("play movie wait"),NULL,NULL,NULL); //播放
//Sleep(80);
mciSendString(_T("stop movie"),NULL,NULL,NULL);
mciSendString(_T("close movie"),NULL,NULL,NULL);
}
用WaitForSingleObjct看看线程是否结束
我加了 WaitForSingleObjct 后,响应十多次后,程序就不动了,假死状态
1 楼,消息处理不当?请问怎么讲,我用的sendmessage,然后会执行 OnPlayMusic()然后这里面创建进程
有关闭啊 mciSendString(_T("close movie"),NULL,NULL,NULL);
这句就是啊
创建的线程句柄最好做记录,退出程序的时候做检查和判别
当CreateThread建立时,系统内部有两个计数器,一是CreateThread本身计数,一是线程本身(运行完成自动释放)
最好在消息处理之前加个Sleep(1);或是通过事件/标志当前一个线程结束时才处理这个
消息
The thread 'Win32 Thread' (0xc78) has exited with code 1 (0x1).
Detected memory leaks!
Dumping objects ->
{9450} normal block at 0x02F54140, 190 bytes long.
Data: < s V V > 04 9A 73 00 56 00 00 00 56 00 00 00 01 00 00 00
{9448} normal block at 0x02F53DF0, 190 bytes long.
Data: < s V V > 04 9A 73 00 56 00 00 00 56 00 00 00 01 00 00 00
{9446} normal block at 0x02F53AA0, 190 bytes long.
Data: < s V V > 04 9A 73 00 56 00 00 00 56 00 00 00 01 00 00 00
{9444} normal block at 0x02F53750, 190 bytes long.
Data: < s V V > 04 9A 73 00 56 00 00 00 56 00 00 00 01 00 00 00
{9442} normal block at 0x02F59AC8, 190 bytes long.
Data: < s V V > 04 9A 73 00 56 00 00 00 56 00 00 00 01 00 00 00
{9440} normal block at 0x02F53400, 190 bytes long.
Data: < s V V > 04 9A 73 00 56 00 00 00 56 00 00 00 01 00 00 00
{9430} normal block at 0x02F59410, 190 bytes long.
Data: < s V V > 04 9A 73 00 56 00 00 00 56 00 00 00 01 00 00 00
{9428} normal block at 0x02F590C0, 190 bytes long.
Data: < s V V > 04 9A 73 00 56 00 00 00 56 00 00 00 01 00 00 00
{9426} normal block at 0x02F58D70, 190 bytes long.
Data: < s V V > 04 9A 73 00 56 00 00 00 56 00 00 00 01 00 00 00
{9424} normal block at 0x02F58A20, 190 bytes long.
Data: < s V V > 04 9A 73 00 56 00 00 00 56 00 00 00 01 00 00 00
{9422} normal block at 0x02F586D0, 190 bytes long.
Data: < s V V > 04 9A 73 00 56 00 00 00 56 00 00 00 01 00 00 00
{9420} normal block at 0x02F58380, 190 bytes long.
Data: < s V V > 04 9A 73 00 56 00 00 00 56 00 00 00 01 00 00 00
{9418} normal block at 0x02F58030, 190 bytes long.
Data: < s V V > 04 9A 73 00 56 00 00 00 56 00 00 00 01 00 00 00
{9416} normal block at 0x02F57CE0, 190 bytes long.
The thread 'Win32 Thread' (0xa00) has exited with code 0 (0x0).
Data: < s V V > 04 9A 73 00 56 00 00 00 56 00 00 00 01 00 00 00
{9414} normal block at 0x02F57990, 190 bytes long.
Data: < s V V > 04 9A 73 00 56 00 00 00 56 00 00 00 01 00 00 00
{9412} normal block at 0x02F57640, 190 bytes long.
Data: < s V V > 04 9A 73 00 56 00 00 00 56 00 00 00 01 00 00 00
{9410} normal block at 0x02F572F0, 190 bytes long.
Data: < s V V > 04 9A 73 00 56 00 00 00 56 00 00 00 01 00 00 00
{9408} normal block at 0x02F56FA0, 190 bytes long.
Data: < s V V > 04 9A 73 00 56 00 00 00 56 00 00 00 01 00 00 00
{9406} normal block at 0x02F56C50, 190 bytes long.
Data: < s V V > 04 9A 73 00 56 00 00 00 56 00 00 00 01 00 00 00
{9404} normal block at 0x02F56900, 190 bytes long.
Data: < s V V > 04 9A 73 00 56 00 00 00 56 00 00 00 01 00 00 00
{9402} normal block at 0x02F565B0, 190 bytes long.
Data: < s V V > 04 9A 73 00 56 00 00 00 56 00 00 00 01 00 00 00
{9400} normal block at 0x02F56260, 190 bytes long.
Data: < s V V > 04 9A 73 00 56 00 00 00 56 00 00 00 01 00 00 00
{9398} normal block at 0x02F55F10, 190 bytes long.
Data: < s V V > 04 9A 73 00 56 00 00 00 56 00 00 00 01 00 00 00
{9396} normal block at 0x02F55BC0, 190 bytes long.
Data: < s V V > 04 9A 73 00 56 00 00 00 56 00 00 00 01 00 00 00
{9394} normal block at 0x02F55870, 190 bytes long.
Data: < s V V > 04 9A 73 00 56 00 00 00 56 00 00 00 01 00 00 00
{9392} normal block at 0x02F55520, 190 bytes long.
Data: < s V V > 04 9A 73 00 56 00 00 00 56 00 00 00 01 00 00 00
Object dump complete.问题是,加了顶楼的代码,退出程序这段提示就出来了