//这个是自定义消息 收到消息就执行函数,就是创建线程播放音乐,
//这样当多收到几次这个消息后,就有内存漏泄
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);
}

解决方案 »

  1.   

    怎么知道泄漏了?
    用WaitForSingleObjct看看线程是否结束
      

  2.   

    程序退出 vs 的output窗口说 检测到有内存泄漏
    我加了 WaitForSingleObjct 后,响应十多次后,程序就不动了,假死状态
    1 楼,消息处理不当?请问怎么讲,我用的sendmessage,然后会执行 OnPlayMusic()然后这里面创建进程
      

  3.   

    俺也看不出来哪泄露了mciSendString这个打开了文件要不要关闭呢好久没搞播放的了
      

  4.   


    有关闭啊  mciSendString(_T("close movie"),NULL,NULL,NULL);
    这句就是啊
      

  5.   

    请问这是个mciSendString什么函数,是不是这个函数里面泄露了?
      

  6.   

    ResumeThread 传递的参数应该是线程句柄,即由CreateThread创建的进程得到的句柄
    创建的线程句柄最好做记录,退出程序的时候做检查和判别
      

  7.   

    把createthread 改成 _beginthreadex 试试
      

  8.   

    CreateThread 返回的HANDLE需要CLOSE()释放掉
      

  9.   

    我也觉得问题就在这,创建CreateThread接着就是要释放
    当CreateThread建立时,系统内部有两个计数器,一是CreateThread本身计数,一是线程本身(运行完成自动释放)
      

  10.   

    如果消息过多, 就可能创建多个线程,内存也会增加, 线程过多时, 程序也会当掉,
    最好在消息处理之前加个Sleep(1);或是通过事件/标志当前一个线程结束时才处理这个
    消息
      

  11.   

    有新的进展了,应该不是这段代码的问题,没有这段代码,有个线退出的代码是 0x02,但没有以下提示
    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.问题是,加了顶楼的代码,退出程序这段提示就出来了