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.
I'd use std::string
throughout.
You are calling strdup
in the loop in the top function and you are never free
'ing those strings you are allocating.
My guess would be that the memory allocated by _strdup isn't freed.
精彩评论