我正在运行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