以下是代碼片斷:.h文件中定義
struct INTOTHREAD
{
void* i;
char c[64];
};
INTOTHREAD* m_intothread;.cpp中使用
//將這個字符保存到結構体中
m_intothread = NULL;
m_intothread = new INTOTHREAD;

strcpy(m_intothread->c,cfileindex);
m_intothread->i = this;(對話框CChatDlg的指針)

AfxBeginThread(ThreadStartSendPicture,(LPVOID)m_intothread);UINT CChatDlg::ThreadStartSendPicture(LPVOID pParam)
{
//使用while循環在這裡,為了和發送文字互斥
//將這個字符保存到結構体中
INTOTHREAD* intothread = NULL;
intothread = new INTOTHREAD; intothread  = (INTOTHREAD*)pParam; CChatDlg* pDlg=(CChatDlg*)(intothread->i); char cfileindex[32] = {NULL};
strcpy(cfileindex,(char*)(intothread->c));
SendPicture(cfileindex);
delete intothread;
intothread = NULL;
return 0;

}Cpp
void CChatDlg::OnDestroy()函數中釋放
if (m_intothread) 
{
delete m_intothread;    出錯的地方
m_intothread = NULL;
}每次關閉對話框就會出錯
出錯信息關鍵是:
File:dbgheap.c
Line:1050
查找call stack,指向OnDestroy()函數中的delete m_intothread;如果去掉OnDestroy()函數中的釋放delete m_intothread;,就不會有問題了
這是爲什麽呢?

解决方案 »

  1.   

    我想strcpy越界是不會有的!char * cfileindex;
    cfileindex = strTempdwUserThread.GetBuffer(strTempdwUserThread.GetLength());
    strTempdwUserThread.ReleaseBuffer();這個是cfileindex獲得的方法,其中的strTempdwUserThread是一定不會超過64個字節的。
      

  2.   

    char cfileindex[32] = {NULL};
      

  3.   

    查一查delete时 m_intothread所指向的空间是否正确。是否被其它的什么东西给覆盖了!
    造成m_intothread指向非法地址。如果可以的话你把你的程序发给我,我帮你调一下!
    [email protected] 标题注明一下vc工程,否则直接删掉了
      

  4.   

    INTOTHREAD* intothread = NULL;
    intothread = new INTOTHREAD; intothread  = (INTOTHREAD*)pParam;我今天是第二次看到这样的代码了
    先new出来,然后将指针指向另外一个结构
    memory leak
      

  5.   

    我觉得pipilupzj(皮皮鲁)说的有道理
    而且,
    void CChatDlg::OnDestroy()
    if (m_intothread) 
    {
    m_intothread = NULL;
    delete m_intothread;
    }
      

  6.   

    這個問題大家公認了,是第二次的new不應該分配的!但是不懂的是:
     sujinzhao407(摇风清影)的刪除的方法有些不理解:void CChatDlg::OnDestroy()
    if (m_intothread) 
    {
    m_intothread = NULL;
    delete m_intothread;
    }為什麽不能像下面這樣呢?
    void CChatDlg::OnDestroy()
    if (m_intothread) 
    {
    delete m_intothread;
    m_intothread = NULL;}