[英]memory leak or some other problem in C++ and vector of strings
i keep getting memory leaks everytime I run this function in a loop: 每当我在循环中运行此函数时,都会不断出现内存泄漏:
uint32_t *padMessages(uint32_t *messages, const std::vector<std::string> &words)
{
uint32_t output[16];
uint32_t size = words.size();
uint32_t i, x, y;
messages = (uint32_t *)malloc(16 * size * sizeof(uint32_t));
x = 0;
for(i = 0; i < size; i++) {
padMessage((uint32_t *)_strdup(words[i].c_str()), output, strlen((const char *)words[i].c_str()) * 8);
for(y = 0; y < 16; y++) {
messages[x] = output[y];
x++;
}
}
return messages;
}
the code calling this function is 调用此函数的代码是
do {
for(i=0 ; i<len ; i++)
temp[i] = letters[entry[i]];
temp[len] = '\0';
// store generated word to words vector
words[array_count] = temp;
// increment vector counter and total word count
array_count++;
word_count++;
if(array_count == 100000) {
// reinitialize array_count
array_count = 0;
// pad messages
messages = padMessages(messages, words);
free(messages);
free(result);
std::vector<std::string>().swap(words);
words.resize(100000);
}
for(i=0 ; i<len && ++entry[i] == nbletters; i++) entry[i] = 0;
} while(i<len);
where is my memory leaking? 我的内存在哪里泄漏?
This: 这个:
(uint32_t *)_strdup(words[i].c_str())
...allocates memory dynamically, and I don't see any matching call to free
for the memory allocated. ...动态分配内存,我看不到任何匹配的调用来free
已分配的内存。
I'd use std::string
throughout. 我会一直使用std::string
。
您在top函数的循环中调用strdup
,并且永远不会free
正在分配的字符串。
我的猜测是_strdup分配的内存没有释放。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.