我如何解决堆栈溢出或为其添加更多内存在此线程函数中 [英] How I Solve stack overflow or add more memory for it In This Threading Function

查看:84
本文介绍了我如何解决堆栈溢出或为其添加更多内存在此线程函数中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 CTCPClient * m_Client =  new  CTCPClient(); 
MTSOutputDebugString( Huawei-zte Switch SendCommand);
CTL1Response * reponse = new CTL1Response(m_Manager, HU);
reponse-> m_Tl1ParamsMap = this-> m_Tl1Map;
strcpy(reponse-> m_technologyid, AX);

strcpy(h.ip,msan.ip);
h.port = msan.port;

reponse-> THip = h.ip;
reponse-> THport = h.port;
// string switch_uname =root;
// string switch_pwd =root;


m_Client-> Bind( );
errorno = m_Client-> Connect(h.ip,h.port);

if (errorno!= 1
{
MTSOutputDebugString( HUAWEI-zte MSAN Connection Failed);

reponse-> CreateResponse(ResponseString,NO_AVAILABLE_TESTHEAD);

return ResponseString;
}
else
{

MTSOutputDebugString( HUAWEI-zte MSAN Connection sucess \\\
);
}

char * bbuf = new char [ 1000 ];
pots = config\r\ n;

m_Client-> Write(username.c_str());
m_Client->写( \\\\ n);
errorno = m_Client->读取(bbuf, 300 12 1 ,& mread);

m_Client-> Write((char *)pwd.c_str());
m_Client->写( \\\\ n);
errorno = m_Client->读取(bbuf, 800 6 1 ,& mread);

MTSOutputDebugString((char *)bbuf);
m_Client->写((char *)pots.c_str());
m_Client->读取(bbuf, 200 5 1 ,& mread);
MTSOutputDebugString((char *)bbuf);
if (errorno< 1)
{
m_Client-> Disconnect();
删除m_Client;
return HUAWEI-zte MSAN发送花盆失败;
}
/// ////////////// //////
m_Client->写入( ag\\ \
);
errorno = m_Client->读取(bbuf, 800 5 1 ,& mread);
int * shelf; shelf = new int ;
int * rac; rac = new int ;
int * slot; slot = new int ;
* shelf = 3 ;
* rac = 1 ;
* slot = 2 ;
/// ///未经测试开始

< span class =code-keyword> int
circit = terminalid ;;
terminalid / = 32 ;

if (terminalid > 0
{
// * slot = terminalid;
terminalid * = 32 ;
circit - = terminalid;
}
m_Manager-> m_DB.zte_get(terminalid,( int &)* rac,( int &)* shelf,( int &)* slot);

sleep( 5 );
/// ///////
char * num = new char [ 4 ];
// int num1 [4];
pots = get-sunitinfo rack + string(itoa(* rac,num, 10 ))+ shelf + string(itoa(* shelf,num,< span class =code-digit> 10
))+ slot + string (itoa(* slot,num, 10 ))+ ;
m_Client->写(pots.c_str());
m_Client->写( \\\\ n);
// sleep(5);
/// //////////////////////// * /
pots = 开始项目29次1强制0单位 +解析(bbuf, - 2)+ sunit +解析(bbuf,-3)+ index + string(itoa(circit,num, 10 )) + \\\\ n; // get out voltage

delete [] num;
删除货架;删除槽;删除rac;

m_Client->写入( exit\r\ n);
errorno = m_Client->读取(bbuf, 800 3 1 ,& mread);
m_Client->写( 112 \\ n);
errorno = m_Client->读取(bbuf, 100 3 1 ,& mread);


m_Client-> Write((char *)pots.c_str(), true );

errorno = m_Client-> Read(bbuf, 600 6 RESULT 1 ,& MREAD);
sleep( 7 );
errorno = m_Client->读取(bbuf, 1000 18 sizeof (bbuf),& mread );

if (errorno< 1)
{
delete [] bbuf;
m_Client-> Disconnect();
删除m_Client;
reponse-> CreateResponse(ResponseString,NO_AVAILABLE_TESTHEAD);

}
if (strstr(bbuf, 用户忙)!= NULL)
{
delete [] bbuf;
m_Client-> Disconnect();
删除m_Client;
reponse-> CreateResponse(ResponseString,LINE_IS_BUSY);

return ResponseString;
}

pots = string (bbuf);
m_Client->写( exit\r\ n);
errorno = m_Client->读取(bbuf, 100 5 1 ,& mread);
m_Client->写( logout \\\\ n);
m_Client-> Disconnect();
delete [] bbuf; // free(bbuf);
MTSOutputDebugString( \ n test \ n);
m_Manager-> dbSection.Lock();
删除m_Client;
m_Manager-> dbSection.Unlock();
MTSOutputDebugString( \ n test \ n);

if (atoi(GetResultValue(pots, 总失败次数:)。c_str())> 0
{
MTSOutputDebugString( \ nFail \ n) ;
delete [] bbuf;
m_Client-> Disconnect();
reponse-> CreateResponse(ResponseString,NO_AVAILABLE_TESTHEAD);
删除m_Client;
delete reponse-> m_GData;
delete reponse-> m_db;
delete reponse-> m_cs;
删除回复;
// sleep(3);
返回 ResponseString;

}

// bool busy = true;
reponse-> m_GData-> dcvoldata.b_ae = true ;
// ,:);
reponse-> m_GData- > dcvoldata.sae = GetResultValue(pots, A-GND DC Vol:);
/// //////

< span class =code-comment> //
pots =;
// / /////////////////////////////////// /////////

// sleep

解决方案

从代码中间开始:

 < span class =code-keyword> if (errorno< 1)
{
delete [] bbuf;
m_Client-> Disconnect();
delete m_Client;
reponse-> CreateResponse(ResponseString,NO_AVAILABLE_TESTHEAD);

}
if (strstr(bbuf, 用户忙)!= NULL)
{
delete [] bbuf;
m_Client-> Disconnect();
delete m_Client;
reponse-> CreateResponse(ResponseString,LINE_IS_BUSY);

return ResponseString;
}



第一个删除bbuf,但第二个如果再次访问它,导致访问异常。你也删除了第一个if块中的m_Client,虽然你以后仍然可以访问它


当你不需要时不要使用new / delete。



用简单的int声明替换它们......



  int  shelf =  3 ; 
int rac = 1 ;
int slot = 2 ;





...并相应地调整对这些变量的引用。



替换:



  char  * bbuf =  new   char  [ 1000 ]; 





... with ...



  char  pbuf [  1000 ] = { 0 }; 





...并删除delete [] pbuf语句。



替换:



  char  num =  new   char  [ 4 ]; 





... ... ...



  char  num [ 4 ] = { 0 } ; 





...并删除delete [] num语句。



此外,看起来像条款...



<前lang =c ++> 如果( errorno< 1)





...缺少退货声明......



  return  ResponseString; 


CTCPClient* m_Client = new CTCPClient();
MTSOutputDebugString("Huawei-zte Switch SendCommand");
CTL1Response * reponse = new CTL1Response(m_Manager, "HU");
reponse->m_Tl1ParamsMap=this->m_Tl1Map;
strcpy(reponse->m_technologyid,"AXE");

strcpy(h.ip,msan.ip);
h.port = msan.port;

reponse->THip=h.ip;
reponse->THport=h.port;
//string switch_uname = "root";
//string switch_pwd = "root";


m_Client->Bind();
errorno = m_Client->Connect(h.ip,h.port);

if(errorno != 1)
{
MTSOutputDebugString("HUAWEI-zte MSAN Connection Failed");

reponse->CreateResponse(ResponseString,NO_AVAILABLE_TESTHEAD);

return ResponseString;
}
else
{

MTSOutputDebugString("HUAWEI-zte MSAN Connection sucess \n");
}

char * bbuf = new char [1000];
pots = "config\r\n";

m_Client->Write(username.c_str());
m_Client->Write("\r\n");
errorno = m_Client->Read(bbuf,300,12,":",1,&mread);

m_Client->Write((char*)pwd.c_str());
m_Client->Write("\r\n");
errorno = m_Client->Read(bbuf,800,6,"#",1,&mread);

MTSOutputDebugString((char*)bbuf);
m_Client->Write((char*)pots.c_str());
m_Client->Read(bbuf,200,5,":",1,&mread);
MTSOutputDebugString((char*)bbuf);
if(errorno<1)
{
m_Client->Disconnect();
delete m_Client;
return "HUAWEI-zte MSAN send pots failed ";
}
///////////////////////
m_Client->Write("ag\r\n");
errorno = m_Client->Read(bbuf,800,5,"#",1,&mread);
int * shelf;shelf = new int;
int * rac; rac = new int ;
int* slot;slot = new int ;
*shelf = 3;
* rac = 1;
* slot = 2;
////// not tested begin

int circit = terminalid;;
terminalid /= 32;

if(terminalid > 0)
{
//*slot = terminalid;
terminalid *= 32;
circit -=terminalid;
}
m_Manager->m_DB.zte_get(terminalid,(int &)*rac,(int &)*shelf,(int &)*slot);

sleep(5);
//////////
char * num = new char[4];
//int num1[4];
pots = "get-sunitinfo rack "+string(itoa(*rac,num,10))+" shelf "+string(itoa(*shelf,num,10))+" slot "+string(itoa(*slot,num,10))+"";
m_Client->Write(pots.c_str());
m_Client->Write("\r\n");
//sleep(5);
///////////////////////////*/
pots = "start item 29 times 1 obliged 0 unit "+ parsing(bbuf,-2)+" sunit "+parsing(bbuf,-3)+" index "+string(itoa(circit,num,10))+" \r\n" ; // get out voltage

delete []num;
delete shelf;delete slot;delete rac;

m_Client->Write("exit\r\n");
errorno = m_Client->Read(bbuf,800,3,"#",1,&mread);
m_Client->Write("112\r\n");
errorno = m_Client->Read(bbuf,100,3,"#",1,&mread);


m_Client->Write((char*)pots.c_str(),true);

errorno = m_Client->Read(bbuf,600,6,"RESULT",1,&mread);
sleep(7);
errorno = m_Client->Read(bbuf,1000,18,"#",sizeof(bbuf),&mread);

if(errorno<1)
{
delete []bbuf;
m_Client->Disconnect();
delete m_Client;
reponse->CreateResponse(ResponseString,NO_AVAILABLE_TESTHEAD);

}
if(strstr(bbuf,"User busy")!=NULL)
{
delete []bbuf;
m_Client->Disconnect();
delete m_Client;
reponse->CreateResponse(ResponseString,LINE_IS_BUSY);

return ResponseString;
}

pots = string(bbuf);
m_Client->Write("exit\r\n");
errorno = m_Client->Read(bbuf,100,5,"#",1,&mread);
m_Client->Write("logout\r\n");
m_Client->Disconnect();
delete []bbuf;//free(bbuf);
MTSOutputDebugString("\n test\n");
m_Manager->dbSection.Lock();
delete m_Client ;
m_Manager->dbSection.Unlock();
MTSOutputDebugString("\n test\n");

if(atoi(GetResultValue(pots,"Total failure count :").c_str())> 0 )
{
MTSOutputDebugString("\nFail\n");
delete []bbuf;
m_Client->Disconnect();
reponse->CreateResponse(ResponseString,NO_AVAILABLE_TESTHEAD);
delete m_Client;
delete reponse->m_GData;
delete reponse->m_db;
delete reponse->m_cs;
delete reponse;
// sleep(3);
return ResponseString;

}

//bool busy = true;
reponse->m_GData->dcvoldata.b_ae = true;
//,":");
reponse->m_GData->dcvoldata.sae = GetResultValue(pots,"A-GND DC Vol:");
/////////

//pots = "";
///////////////////////////////////////////////

//   sleep

解决方案

From the middle of your code:

if(errorno<1)
{
delete []bbuf;
m_Client->Disconnect();
delete m_Client;
reponse->CreateResponse(ResponseString,NO_AVAILABLE_TESTHEAD);

}
if(strstr(bbuf,"User busy")!=NULL)
{
delete []bbuf;
m_Client->Disconnect();
delete m_Client;
reponse->CreateResponse(ResponseString,LINE_IS_BUSY);

return ResponseString;
}


The first if deletes bbuf, but then the second if accesses it again, leading to access exception. Also you delete m_Client in the first if block although you still access it later


Don't use new/delete when you don't need them.

Replace these with simple int declarations ...

int shelf = 3;
int rac = 1;
int slot = 2;



... and adjust references to these variables accordingly.

Replace:

char * bbuf = new char [1000];



... with ...

char pbuf[1000] = {0};



... and remove the delete[] pbuf statements.

Replace:

char num = new char[4];



... with ...

char num[4] = {0};



... and remove the delete[] num statements.

Also, looks like clause ...

if(errorno<1)



... is missing a return statement ...

return ResponseString;


这篇关于我如何解决堆栈溢出或为其添加更多内存在此线程函数中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆