Bonjour.
Valgrind m'indique un invalid free dans mon code (fichier harmony.c) , et je ne comprends pas pourquoi.
Pour comprendre, voici le type Note_t utilisé:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8 struct _note{ Pitch_t pitch; int midipitch; double start; double length; double weight; }; typedef struct _note *Note_t;
Voici le rapport valgrind
Et voici mon code issu de harmony.c (en gras les lignes 468 et 464, qui sont visées par valgrind):==29053== Invalid free() / delete / delete[]
==29053== at 0x402123A: free (vg_replace_malloc.c:233)
==29053== by 0x8057D60: noteChord_analysis (harmony.c:468)
==29053== by 0x805639A: harmony (harmony.c:60)
==29053== by 0x8059D43: harmony_cmd (cmd.c:58)
==29053== by 0x805A281: com_harmony (tonality.c:395)
==29053== by 0x8059FB6: execute_line (tonality.c:188)
==29053== by 0x805A7A1: main (tonality.c:144)
==29053== Address 0x4ACC1C0 is 0 bytes inside a block of size 32 free'd
==29053== at 0x402123A: free (vg_replace_malloc.c:233)
==29053== by 0x8057D2C: noteChord_analysis (harmony.c:464)
==29053== by 0x805639A: harmony (harmony.c:60)
==29053== by 0x8059D43: harmony_cmd (cmd.c:58)
==29053== by 0x805A281: com_harmony (tonality.c:395)
==29053== by 0x8059FB6: execute_line (tonality.c:188)
==29053== by 0x805A7A1: main (tonality.c:144)
Voilà, je comprends rien. Visiblement, le second free libère un truc déjà libéré par le premier, mais je vois vraiment pas pourquoi.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36 int noteChord_analysis(NoteChord_t *noteChords, Score_t score, int nb_tracks, MidiFile_t midifile){ int noteChords_size=0; int time=0; int next_time; int final_time=get_final_time(score, nb_tracks); int n_index=0; int j; int min_l=0; // min length for a noteChord, due to homorhythmic transformation Note_t *noteA=(Note_t*)malloc(500*sizeof(Note_t)); for(j=0;j<500;j++){ noteA[j]=(Note_t )malloc(sizeof(struct _note)); } Note_t *noteB=(Note_t*)malloc(100*sizeof(Note_t)); for(j=0;j<100;j++){ noteB[j]=(Note_t )malloc(sizeof(struct _note)); } while (time <= final_time){ noteChords[n_index]->nb_notes=get_instant_notes(noteA, time, nb_tracks, score); min_l=get_min_length_among_notes(noteA, noteChords[n_index]->nb_notes); next_time=get_next_implicit_time(noteB,time, min_l, nb_tracks, score); fill_noteChord_pitches(noteA, noteChords[n_index]->nb_notes, noteChords, n_index, time, next_time-time); fill_noteChord_bass_and_root(noteChords, n_index); time=next_time; n_index ++; noteChords_size++; } for(j=0;j<500;j++){ free(noteA[j]); } free(noteA); for(j=0;j<100;j++){ free(noteB[j]); } free(noteB); return noteChords_size; }
Il n'y a pas de dépassement, et valgrind ne me sort que cette erreur, à part lorsque le programme se termine, où il me sort celle là en plus:
Mais cette erreur là, je pense que c'est une conséquence de la première, parceque le fichier score.c ne comporte pas d'erreur de mémoire, il a été testé et retesté quand on l'a conçu il y a plusieurs moi, et valgrind bronche pas lorsque je le fais tourner seul.==29053== Invalid free() / delete / delete[]
==29053== at 0x402123A: free (vg_replace_malloc.c:233)
==29053== by 0x804D309: destroy_score (score.c:45)
==29053== by 0x80564B7: harmony (harmony.c:96)
==29053== by 0x8059D43: harmony_cmd (cmd.c:58)
==29053== by 0x805A281: com_harmony (tonality.c:395)
==29053== by 0x8059FB6: execute_line (tonality.c:188)
==29053== by 0x805A7A1: main (tonality.c:144)
==29053== Address 0x4ACC1C0 is not stack'd, malloc'd or (recently) free'd
Je peux bien sur communiquer tout le fichier harmony.c (et d'autres fichiers) à qui l'estimerait necessaire pour trouver le problème.
Ah oui, je peux préciser, avant, mon programme marchait nickel, sauf que noteA et noteB était déclarés comme suit:
Note_t noteA[500];
Note_t noteB[500];
cela n'aurait jamais du marcher je suis d'accord, car seul un tableau de pointeur était déclaré, et aucun emplacement n'était réservé pour les structures, mais bon... ça marchait, ce qui est assez bizarre... et maintenant que je veux réparer cette erreur, et ben ça marche plus... du tout....
Partager