当前位置: 代码迷 >> VC/MFC >> exe has triggered a breakpoint求解,该怎么解决
  详细解决方案

exe has triggered a breakpoint求解,该怎么解决

热度:102   发布时间:2016-05-02 03:27:42.0
exe has triggered a breakpoint求解

DWORD WINAPI RevFile(LPVOID p)
{
if (iPic > 0)
{
return 0;
}

clientSt *pStClient = (clientSt *)p;
BOOL bKeep = TRUE;
while (bKeep)
{
char revBuffer[1024];
memset(revBuffer, 0, sizeof(revBuffer));
int nBufferSize = recv(pStClient->clientSocket, revBuffer, sizeof(revBuffer), 0);
if (strlen(revBuffer) == 0)
{
bKeep = FALSE;
continue;
}

std::vector<char *> vString;
int pos = 0, prePos = 0;
while (pos < nBufferSize - 1)
{
if (strncmp(revBuffer+pos, "%7C", 3) == 0)
{
char *p = NULL;
if (prePos > 0)
{
p = new char[pos-prePos];
strncpy(p, revBuffer+prePos+1, pos-prePos-1);
p[pos-prePos-1] = '\0';
prePos = pos+2;
}
else
{
p = new char[pos+1];
strncpy(p, revBuffer, pos);
p[pos] = '\0';
prePos = pos+2;
}

vString.push_back(p);
}

pos++;
}

if (vString.size() > 0)
{
//获取客户端IP地址
SOCKADDR_IN sin;
memset(&sin, 0, sizeof(sin));
int nLen = sizeof(sin);
getpeername(pStClient->clientSocket, (SOCKADDR*)&sin, &nLen);
char szIpAddr[32];
strcpy(szIpAddr, inet_ntoa(sin.sin_addr));
CString strIpAddr = (const TCHAR *)_bstr_t(szIpAddr);

//判断传输数据类型
if (strcmp(vString[0], "PHONECONNECT") == 0)
{
BOOL bHave = FALSE;
for (int i = 0; i < pStClient->pDlg->m_listPhone.GetItemCount(); i++)
{
CString strItemIP = pStClient->pDlg->m_listPhone.GetItemText(i, iIP);
if (strItemIP.Compare(strIpAddr) == 0)
{
bHave = TRUE;
break;
}
}

if (!bHave)
{
int iItem = pStClient->pDlg->m_listPhone.GetItemCount();

CString strSort;
strSort.Format("%d", iItem+1);

pStClient->pDlg->m_listPhone.InsertItem(iItem, strSort);

CString strClientName = (const TCHAR *)_bstr_t(vString[1]);
pStClient->pDlg->m_listPhone.SetItemText(iItem, iUser, strClientName);
pStClient->pDlg->m_listPhone.SetItemText(iItem, iIP, strIpAddr);
pStClient->pDlg->m_listPhone.SetItemText(iItem, iStatus, "在线");

pStClient->pDlg->SendMessage(WM_MY_CREATE_DLG, (WPARAM)(&strIpAddr));
}
}
else if (strcmp(vString[0], "PHONEDISCONNECT") == 0)
{
int iPhoneCount = pStClient->pDlg->m_listPhone.GetItemCount();
for (int i = iPhoneCount - 1; i >= 0; i--)
{
CString strIPAddress = pStClient->pDlg->m_listPhone.GetItemText(i, iIP);
if (strIpAddr.Compare(strIPAddress) == 0)
{
CPhoneView *p = m_mapIP2View[strIPAddress];
ASSERT(p);
delete p;

pStClient->pDlg->m_listPhone.DeleteItem(i);
}
}

bKeep = FALSE;
}
else if (strcmp(vString[0], "PHONEVIDEO") == 0)
{
//iPic++;
int bufSize = 1024 * 1024;
char *pBufer = new char[bufSize];
memset(pBufer, 0, bufSize);

int nHeadLen = strlen(vString[0]) + strlen(vString[1]) + 6;
    int rightSize = nBufferSize-nHeadLen;
memcpy(pBufer, revBuffer+nHeadLen, rightSize);
//memset(revBuffer, 0, sizeof(revBuffer));
int len = 0, count = 0;
while((len = recv(pStClient->clientSocket, pBufer+rightSize+count, 512, 0)) > 0)
{
count += len;
}

count += rightSize;

HGLOBAL m_hMem = GlobalAlloc(GMEM_FIXED, count);
BYTE* pMem = (BYTE*)GlobalLock(m_hMem);
memcpy(pMem, pBufer, count);
delete[] pBufer;

IStream *pStmBmp = NULL;
CreateStreamOnHGlobal(m_hMem, FALSE, &pStmBmp);
if (pStmBmp == NULL)
{
GlobalFree(m_hMem);
return 0;
}

Gdiplus::Image *pCurImg = Gdiplus::Image::FromStream(pStmBmp, FALSE);
GlobalUnlock(m_hMem);
pStmBmp->Release();
GlobalFree(m_hMem);

pStClient->pDlg->SendMessage(WM_MY_UPDATE_DLG, (WPARAM)(&strIpAddr), (LPARAM)pCurImg);
}
else
{
bKeep = FALSE;
}
}

for (std::size_t i = 0; i < vString.size(); i++)
{
delete []vString[i];
vString[i] = NULL;
}
vString.clear();
}

return 0;
}

DWORD WINAPI ListenThread(LPVOID p)
{
CDlgServerDlg *pDlg = (CDlgServerDlg *)p;
//准备接受请求

if(listen(pDlg->m_hSocket, 10) == SOCKET_ERROR)
{
AfxMessageBox("Listen Error");
closesocket(pDlg->m_hSocket);
return 0;
}

while(1)
{
ASSERT(pDlg);

CString strError;
int error;

SOCKET s = accept(pDlg->m_hSocket, NULL, NULL);
if (s == SOCKET_ERROR)
{
if(pDlg->m_bStatus)
{
strError.Format("Accept Error:%d ",error=WSAGetLastError());
AfxMessageBox(strError);
closesocket(pDlg->m_hSocket);
return -1;
}
else
{
break;
}
}

clientSt *pStClient = new clientSt;
pStClient->pDlg = pDlg;
pStClient->clientSocket = s;

pDlg->m_hClientThread = CreateThread(NULL, 0, RevFile, (LPVOID)pStClient, 0, NULL);
}

return 0;
}


图片传输到一定程度的时候就会出现 exe has triggered a breakpoint 问题,百度说是内存问题,可是我找不出来这段代码有什么问题,感谢
------解决思路----------------------
有时不将“调用函数名字+各参数值,进入函数后各参数值,中间变量值,退出函数前准备返回的值,返回函数到调用处后函数名字+各参数值+返回值”这些信息写日志到文件中是无论如何也发现不了问题在哪里的,包括捕获各种异常、写日志到屏幕、单步或设断点或生成core文件、……这些方法都不行! 写日志到文件参考下面:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef WIN32
    #include <windows.h>
    #include <io.h>
#else
    #include <unistd.h>
    #include <sys/time.h>
    #include <pthread.h>
    #define  CRITICAL_SECTION   pthread_mutex_t
    #define  _vsnprintf         vsnprintf
#endif
//Log{
#define MAXLOGSIZE 20000000
#define MAXLINSIZE 16000
#include <time.h>
#include <sys/timeb.h>
#include <stdarg.h>
char logfilename1[]="MyLog1.log";
char logfilename2[]="MyLog2.log";
static char logstr[MAXLINSIZE+1];
char datestr[16];
char timestr[16];
char mss[4];
CRITICAL_SECTION cs_log;
FILE *flog;
#ifdef WIN32
void Lock(CRITICAL_SECTION *l) {
    EnterCriticalSection(l);
}
void Unlock(CRITICAL_SECTION *l) {
    LeaveCriticalSection(l);
}
#else
void Lock(CRITICAL_SECTION *l) {
    pthread_mutex_lock(l);
}
void Unlock(CRITICAL_SECTION *l) {
    pthread_mutex_unlock(l);
}
#endif
void LogV(const char *pszFmt,va_list argp) {
    struct tm *now;
    struct timeb tb;

    if (NULL==pszFmt
------解决思路----------------------
0==pszFmt[0]) return;
    _vsnprintf(logstr,MAXLINSIZE,pszFmt,argp);
    ftime(&tb);
    now=localtime(&tb.time);
    sprintf(datestr,"%04d-%02d-%02d",now->tm_year+1900,now->tm_mon+1,now->tm_mday);
    sprintf(timestr,"%02d:%02d:%02d",now->tm_hour     ,now->tm_min  ,now->tm_sec );
    sprintf(mss,"%03d",tb.millitm);
    printf("%s %s.%s %s",datestr,timestr,mss,logstr);
    flog=fopen(logfilename1,"a");
    if (NULL!=flog) {
        fprintf(flog,"%s %s.%s %s",datestr,timestr,mss,logstr);
        if (ftell(flog)>MAXLOGSIZE) {
            fclose(flog);
            if (rename(logfilename1,logfilename2)) {
                remove(logfilename2);
                rename(logfilename1,logfilename2);
            }
        } else {
            fclose(flog);
        }
    }
}
void Log(const char *pszFmt,...) {
    va_list argp;

    Lock(&cs_log);
    va_start(argp,pszFmt);
    LogV(pszFmt,argp);
    va_end(argp);
    Unlock(&cs_log);
}
//Log}
int main(int argc,char * argv[]) {
    int i;
#ifdef WIN32
    InitializeCriticalSection(&cs_log);
#else
    pthread_mutex_init(&cs_log,NULL);
#endif
    for (i=0;i<10000;i++) {
        Log("This is a Log %04d from FILE:%s LINE:%d\n",i, __FILE__, __LINE__);
    }
#ifdef WIN32
    DeleteCriticalSection(&cs_log);
#else
    pthread_mutex_destroy(&cs_log);
#endif
    return 0;
}
//1-78行添加到你带main的.c或.cpp的那个文件的最前面
//81-85行添加到你的main函数开头
//89-93行添加到你的main函数结束前
//在要写LOG的地方仿照第87行的写法写LOG到文件MyLog1.log中

------解决思路----------------------
出错的时候,点击重试,编译器打开,就会定位到对应的代码
然后你看看你的phoneview.cpp对应的行数的代码
------解决思路----------------------
Debug下打开call stack,看看函数调用堆栈,先找到出错的函数调用。