Unable to free memory after using strdup

gcc 4.5.1 c89

I'm trying to free my memory. However, when I check valgrind, the memory was not freed. I am wondering what I am doing wrong.

I have the following structure:

typedef struct tag_cand_results {
    char *candidate_winners[NUMBER_OF_CANDIDATES];
} cand_results;

I create an object of this structure:

cand_results *results = NULL;

I allocate some memory for the structure.

results = calloc(1, sizeof *results);

Assign him some data

results->candidate_winners[0] = strdup("Steve Martin");
results->candidate_winners[1] = strdup("Jack Jones");

Then I try to free all the allocated memory:

free(results->candidate_winners[0]);
free(results->candidate_winners[1]);
free(results);

Just to be safe assign to NULL
results = NULL;

I get the following result from valgrind.

==8119== 72 bytes in 6 blocks are definitely lost in loss record 1 of 2
==8119==    at 0x4A05E46: malloc (vg_replace_malloc.c:195)
==8119==    by 0x3FE2E82A91: strdup (strdup.c:43)
==8119==    by 0x400E5A: main (driver.c:116)
==8119== 
==8119== 72 bytes in 6 blocks are definitely lost in loss record 2 of 2
==8119==    at 0x4A05E46: malloc (vg_replace_malloc.c:195)
==8119==    by 0x3FE2E82A91: strdup (strdup.c:43)
==8119==    by 0x400E72: main (driver.c:117)

I do not know why the memory was not freed?

Thanks so much for any suggestions,

+3
source share
4 answers

If this is actually a sequence of events, then valgrind is wrong. The memory is freed.


, , valgrind, , , : -)

.

  • , malloc(30) strdup(some_string) ( )?
  • (malloc-or-strdup)/free pairs , , .
  • , printf strdup free, , .
  • ( ), .

() :

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define NUMBER_OF_CANDIDATES 10
typedef struct tag_cand_results {
    char *candidate_winners[NUMBER_OF_CANDIDATES];
} cand_results;

int main (void) {
    cand_results *results = NULL;

    results = calloc(1, sizeof *results);

    results->candidate_winners[0] = strdup("Steve Martin");
    results->candidate_winners[1] = strdup("Jack Jones");

    free(results->candidate_winners[0]);
    free(results->candidate_winners[1]);
    free(results);

    results = NULL;

    return 0;
}

valgrind:

==9649== Memcheck, a memory error detector
==9649== Copyright (C) 2002-2009, and GNU GPL'd, by Julian Seward et al.
==9649== Using Valgrind-3.6.0.SVN-Debian and LibVEX; rerun with -h for
         copyright info
==9649== Command: ./qq
==9649== 
==9649== 
==9649== HEAP SUMMARY:
==9649==     in use at exit: 0 bytes in 0 blocks
==9649==   total heap usage: 3 allocs, 3 frees, 64 bytes allocated
==9649== 
==9649== All heap blocks were freed -- no leaks are possible
==9649== 
==9649== For counts of detected and suppressed errors, rerun with: -v
==9649== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 13 from 8)

, . - (, ). Ubuntu Lucid (10.04), gcc 4.4.3, c89.

, , . , , :

gcc -std=c89 -o qq qq.c
valgrind ./qq
+4

/ .

, - ( - ?).

, ( printf ( "% p",...)) strdup . : bingo!

, , ( ).

, , , ?

. , . , . , .

, , ( ).

, :

assert ( == saved_result);

- , .

, , . , saved_result. , - .

+2

gdb , - "". , , .

,

+2

"72 6 ", " " " ". - (!)?

+1

Source: https://habr.com/ru/post/1780710/


All Articles