I’m using a function I found here to save a webpage to memory with cURL:
struct WebpageData {
char *pageData;
size_t size;
};
size_t storePage(void *input, size_t size, size_t nmemb, void *output) {
size_t realsize = size * nmemb;
struct WebpageData *page = (struct WebpageData *)output;
page->pageData = (char *)realloc(page->pageData, page->size + realsize + 1);
if(page->pageData) {
memcpy(&(page->pageData[page->size]), input, realsize);
page->size += realsize;
page->pageData[page->size] = 0;
}
return realsize;
}
and find the line:
page->pageData = (char *)realloc(page->pageData, page->size + realsize + 1);
is causing a memory leak of a few hundred bytes per call. The only real change I’ve made from the original source is casting the line in question to a (char *), which my compiler (gcc, g++ specifically if it’s a c/c++ issue, but gcc also wouldn’t compile with the uncast statement) insisted upon, but I assume this is the source of the leak. Can anyone elucidate?
Thanks
The code you have posted (as far as I can tell) is correct. If it’s leaking, I
suspect that you are forgetting to
free()the memory block at some point.reallocis allowed to create an entire new memory block if it can’t simplyexpand the existing one, and this is of interest to you. It is also of course allowed to allocate a larger block than needed, which might result in the phantom leaks.
Now, since you are using C++, I have to ask: why aren’t you using
std::vectorinstead?