我正在運行Valgrind檢查我的代碼是否有內存泄漏。 Valgrind沒有顯示任何泄漏發生,但我有一段代碼,我認爲應該導致泄漏,我不明白如何清理變量或Valgrind沒有捕捉它。爲什麼兩個char *數組不會產生泄漏?Char *相關的內存泄漏
void BasicEngine::ConnectionInput(int ConnectionId, const char* ClientInput)
{
// find client assignment to this ConnectionId
Client* thisClient = this->ClientFind(ConnectionId);
int SpaceLocation = strcspn(ClientInput," ");
char* verb;
char* args;
if(SpaceLocation == strlen(ClientInput))
{
verb = (char*)ClientInput;
args = (char*)"";
}
else
{
verb = new char[SpaceLocation+1];
args = new char[strlen(ClientInput)-SpaceLocation+1];
sscanf(ClientInput,"%s %[^\n]",verb,args);
}
if(thisClient != NULL)
{
// ... client is always null, this is not being reached at the moment.
}
else
{
if(this->refCmdHandler != NULL)
if(this->refCmdHandler->cmdHandler(ConnectionId,ClientInput))
return;
}
this->refServer->TransmitNL(ConnectionId,"Invalid Command.");
}
bool BasicCmdProc::cmdHandler(int ConnectionId, string ClientInput)
{
Transmit(ConnectionId,string("You Said: ") + ClientInput);
return true;
}
如果我在 '你好'
輸出輸入:你說:你好
,並沒有檢測到泄漏。
爲什麼你使用的數組不是'vector' s呢? – 2013-02-09 23:46:29
你是不是指'string',@BartekBanachewicz?我想,'vector'是處理字符串的一種不尋常的方式。 –
2013-02-09 23:48:46
這可能是深夜+我最近做了太多奇怪的IO。 – 2013-02-09 23:51:48