I have some code lines which are part of the bigger project. My code compiled with gcc (Debian 4.7.2-5) shows some invalid free/delete
error while being analyzed with valgrind
. However, when I compile it with gcc (Ubuntu/Linaro 4.8.1) the error is gone.
Here's the error message:
==4505== Invalid free() / delete / delete[] / realloc()
==4505== at 0x4C27D4E: free (vg_replace_malloc.c:427)
==4505== by 0x401195: method (test.c:202)
==4505== by 0x401429: main (test.c:264)
==4505== Address 0x401600 is not stack'd, malloc'd or (recently) free'd
==4505==
Linux ==4505==
==4505== HEAP SUMMARY:
==4505== in use at exit: 0 bytes in 0 blocks
==4505== total heap usage: 1,394 allocs, 1,395 frees, 95,379 bytes allocated
==4505==
==4505== All heap blocks were freed -- no leaks are possible
==4505==
==4505== For counts of detected and suppressed errors, rerun with: -v
==4505== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 4 from 4)
And here's the piece of code that fails:
for (i = 0; etcFilenames[i] != NULL; i++)
{
for (j = 0; j < OS_NUM; j++)
{
if (!strcmp(etcFilenames[i], releaseFilenames[j]))
{
filename = (char *)releaseFilenames[j];
char *tmp = readEtcFile(filename);
if (strlen(tmp) == 0)
{
free(tmp);
continue;
}
else
{
name = malloc(sizeof(char) * (strlen(tmp) + 1));
strcpy(name, tmp);
free(tmp);
break;
}
}
}
}
if (uname(&sysInfo) != -1)
{
len = sizeof(sysInfo.sysname) + strlen(name) + 1;
os->sysname = malloc(sizeof(char) * len);
memset (os->sysname, 0, len);
strcpy(sysname, sysInfo.sysname);
strcat(sysname, " ");
strcat(sysname, name);
version = malloc(sizeof(char) * (sizeof(sysInfo.release) + 1));
memset (version, 0, sizeof(sysInfo.release));
strcpy(version, sysInfo.release);
free(name);
}
Valgrind
complains about this last line: free(name);
when all used variables are declared as follows:
struct utsname sysInfo;
FILE *osArch, *distribName;
char *arch, *name = "";
char buff[1024];
int len = 0;
char **etcFilenames = getEtcFilenames();
unsigned int i = 0, j = 0;
const char *releaseFilenames[OS_NUM];
char *filename = "";
My main concern is that I do not know if it is my faul the error came up, or its the gcc issue? How can I know what version is fine? (When I comment out free(name);
on (Debian 4.7.2-5) gcc
its then ok but then gcc 4.8.1
complains about the memory leaks ...)
char *arch, *name = NULL;
Thanks! :) Anyway, when I changed this, I have another issue: pastie.org/private/4esf3ojyidtdfhc34w5zw which refers to the line:len = sizeof(sysInfo.sysname) + strlen(name) + 1;
– mirx