[Valgrind]Probleme de malloc detecté et fuites memoire (petite précision le projet fonctionne )
bonjour ,
j'ai vraiment besoin d'aide, je ne m'en sort pas avec mon projet
avez vous une idée du probleme ?
Citation:
==4941== Invalid write of size 1
==4941== at 0x4026107: strcpy (mc_replace_strmem.c:311)
==4941== by 0x804B8B0: ruleFromString (regle.c:129) / ligne 16 dans le past
==4941== by 0x804EADC: loadRuleBase (persistance.c:212)
==4941== by 0x80492ED: morpionGame (morpion.c:150)
==4941== by 0x804EEAB: main (main.c:37)
==4941== Address 0x46d9842 is 7 bytes after a block of size 3 alloc'd
==4941== at 0x4024F20: malloc (vg_replace_malloc.c:236)
==4941== by 0x804B87F: ruleFromString (regle.c:126)
==4941== by 0x804EADC: loadRuleBase (persistance.c:212)
==4941== by 0x80492ED: morpionGame (morpion.c:150)
==4941== by 0x804EEAB: main (main.c:37)
==4941==
==4941== Invalid read of size 1
==4941== at 0x402665C: strcmp (mc_replace_strmem.c:426)
==4941== by 0x804D556: moteurInfAR (moteur.c:108) // LIGNE 13 dans le past
==4941== by 0x8049950: morpionGame (morpion.c:269)
==4941== by 0x804EEAB: main (main.c:37)
==4941== Address 0x46d983b is 0 bytes after a block of size 3 alloc'd
==4941== at 0x4024F20: malloc (vg_replace_malloc.c:236)
==4941== by 0x804B87F: ruleFromString (regle.c:126)
==4941== by 0x804EADC: loadRuleBase (persistance.c:212)
==4941== by 0x80492ED: morpionGame (morpion.c:150)
==4941== by 0x804EEAB: main (main.c:37)
==4941==
==4941== Invalid read of size 1
==4941== at 0x402665C: strcmp (mc_replace_strmem.c:426)
==4941== by 0x804D556: moteurInfAR (moteur.c:108)
==4941== by 0x8049979: morpionGame (morpion.c:270)
==4941== by 0x804EEAB: main (main.c:37)
==4941== Address 0x46d983b is 0 bytes after a block of size 3 alloc'd
==4941== at 0x4024F20: malloc (vg_replace_malloc.c:236)
==4941== by 0x804B87F: ruleFromString (regle.c:126)
==4941== by 0x804EADC: loadRuleBase (persistance.c:212)
==4941== by 0x80492ED: morpionGame (morpion.c:150)
==4941== by 0x804EEAB: main (main.c:37)
Code:
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
| short ruleFromString( char* ruleString,Rule* r) {
char* token = NULL; // Partie de string
char* hypo = NULL;
short length;
listInit(&(r->hypo));
token = strtok(ruleString, " ");
//parcours des hypotheses
while (token != NULL) {
length = sizeof(char) * (strlen(token)+1);
if(strcmp(token,"->")==0) { // Test : fin des hypothèses
token = strtok(NULL," "); // NULL : prend la derniere chaine de caractères stockée
if(token==NULL)
return -1;
r->concl = malloc((length)); // pb detecte avec valgrind
if(r->concl == NULL) // Test du malloc
return -1;
strcpy(r->concl,token); // pb detecte avec valgrind
return 1;
}
hypo = malloc(length);
strcpy(hypo, token);
if(!listPushBack(&(r->hypo),(void*) hypo)) { // Test fonctionnement de listPushBack ?
listRemoveAll(&(r->hypo)); // Suppression de toutes les hypothèses
return -1;
}
token = strtok(NULL," ");
}
return 1;
} |
Code:
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
| short moteurInfAR(List listRules, List listFacts,char *but) {
Rule *rule = NULL;
char *hypo = NULL;
short flag_allHypoFound = 0;
int i = 0;
int j = 0;
if(elementSearch(listFacts,but) == 1) // Si but est dans les faits
return 1;
for (i = 0; i < listLength(listRules); i++) { // Parcours de la liste de règles
rule = (Rule *) listGetElement(listRules, i);
if (strcmp (but,rule->concl)== 0) { // Si le but est la conclusion d'une des règles
flag_allHypoFound = 1;
for (j = 0; j < listLength(rule->hypo); j++) { // Parcours des hypothèses d'une règle
hypo = (char*) listGetElement(rule->hypo, j);
if (elementSearch(listFacts, hypo) == 0)// Si une hypo n'est pas dans la base de faits ,flag_allHypoFound = 0
flag_allHypoFound = 0;
}
if(flag_allHypoFound) // Si un but est verifié , arrêt du parcourt de la liste regles
break;
}
else
flag_allHypoFound = 0;
}
return flag_allHypoFound;
} |