文件夹里的SEND和RECV,其中RECV是接收文件的,为什么我在RECV中接收完文件后会出现内存错误?附件是我的程序,请帮忙修正好吗

以下是接收文件的源代码:
DWORD WINAPI CBbDlg::RecvFile(LPVOID lpParameter) 
{
    //在上面要加上判断正在接收的文件是否多于5个;
    FileMessage msg;

    memset((char*)&msg,0,sizeof(msg));
    CSocketr*m_sock1=(CSocketr*)lpParameter;
    //delete lpParameter;
    
    m_sock1->Receive(&msg,sizeof(msg));   //这是接受文件的结构信息
    CString str=msg.SendName;
    str+="向你发送文件:";
    str+=msg.FileName;
    int num1;
    int num2;
    CString strl=msg.FileName;
    num2=(int)strl.GetLength();
    num1=strl.Find(".");
    
    CString str1,str2;      //其中STR1为文件名,STR2为后缀名;
    //这里将“.”号分割字符串
    char *ch=new char[num2+2];
    memset(ch,0,num2+2);
    strcpy(ch,strl);
    for(int i=0;i<num1;i++)
    {
        str1+=ch[i];
    }
    
    for(int j=num1+1;j<num2;j++)
    {
        str2+=ch[j];
    }
    
    int Chunk_Size=64*1024;
    if((IDOK==AfxMessageBox(str,MB_OKCANCEL))&&(left<5))
    {
        left++;
        BOOL q=TRUE;
        m_sock1->Send(&q,sizeof(q));
        //还要加上文件选择框
        //加上文件字符串的划分;
        
        CFileDialog fileDlg(FALSE,str2,str1);
        CString TempName;
        if(IDOK==fileDlg.DoModal())
        {
           TempName=fileDlg.GetFileName();
        }
        else
        {
           AfxMessageBox("用户取消文件传输!");
           return 0;
        } 
        
        
        if(msg.FileLong>=Chunk_Size)                //文件大于64K
        {
            
            int idx=0;
            int FileLen=(int)msg.FileLong;

            int nChunkCount=(int)msg.FileLong/Chunk_Size; //文件块数
            if(FileLen%nChunkCount!=0)
            nChunkCount++;
            char *date=new char[Chunk_Size]; //创建数据缓冲区
            CFile file;
            file.Open(TempName,CFile::modeWrite|CFile::typeBinary|CFile::modeCreate);
            //下面是接收过程;
            for(int i=0;i<nChunkCount;i++) //分为nChunkCount块发送
            {
                int nLeft;
                int WriteLong;
                if(i+1==nChunkCount) //最后一块
                {
                    nLeft=FileLen-Chunk_Size*(nChunkCount-1);
                    WriteLong=nLeft;
                }
                else
                {
                    nLeft=Chunk_Size;
                    WriteLong=nLeft;
                }
                int idx=0;
                while(nLeft>0)
                {
                    int ret=m_sock1->Receive(&date[idx],nLeft);//发送文件
                    if(ret==0)return 0;
                    else
                    {
                        nLeft-=ret;
                        idx+=ret;
                    }
                }
                file.Write(date,WriteLong);
            }
            file.Close();
            delete[] date;
            AfxMessageBox("文件传输完成!");
        }
        else        //文件小于64K
        {
           char *date=new char[(int)msg.FileLong]; //分配和文件长度相同的缓冲区
           int idx=0;
           int nLeft=(int)msg.FileLong;

           CFile file;
           file.Open(TempName,CFile::modeWrite|CFile::typeBinary|CFile::modeCreate);
           //int nLeft;
           while(nLeft>0)
           {
               int ret=m_sock1->Receive(&date[idx],nLeft);//发送文件
               if(ret==0)return 0;
               else
               {
                   nLeft-=ret;
                   idx+=ret;
               }
           }
           file.Write(date,(int)msg.FileLong);
           file.Close();
           delete[] date;
           AfxMessageBox("文件传输完成!");
        }
        //m_sock->Close();  //问题出现在这里,可能是悬空指针的问题
    }
    else
    {   
        BOOL q=FALSE;
        m_sock1->Send(&q,sizeof(q));
        //m_sock->Close();
        //int i=0;
    }
    //m_sock->DeleteSock(m_sock1);
//    int r=9;
    delete[] ch;        //要将内存分配多一点才可以用这个???
   // m_sock1->Close();
    //delete m_sock1;
    return 1;
}