c - Trying to free an invalid pointer -
i've got a
*** glibc detected *** [...] free(): invalid pointer: 0x0804d0d0 ***
in function:
ptrgbloque determinar_nodo(const char* path){ // si es el directorio raiz, devuelve 0: if(!strcmp(path, "/")) return 0; int fd, i, nodo_anterior, aux; // super_path usado para obtener la parte superior del path, sin el nombre. char *super_path = (char*) malloc(strlen(path)), *nombre = (char*) malloc(strlen(path)); char *start = nombre, *start_super_path = super_path; //estos liberaran memoria. struct grasa_file_t *node, *inicio; unsigned char *node_name; strcpy(super_path, path); strcpy(nombre, path); // obtiene y acomoda el nombre del archivo. if (lastchar(path, '/')) { nombre[strlen(nombre)-1] = '\0'; } nombre = strrchr(nombre, '/'); nombre[0] = '\0'; nombre = &nombre[1]; // acomoda el nombre, ya que el primer digito siempre es '/' // acomoda el super_path if (lastchar(super_path, '/')) { super_path[strlen(super_path)-1] = '\0'; } aux = strlen(super_path) - strlen(nombre); super_path[aux] = '\0'; nodo_anterior = determinar_nodo(super_path); // abrir conexion y traer directorios, guarda el bloque de inicio para luego liberar memoria if ((fd = open(disc_path, o_rdonly, 0)) == -1) { printf("error"); return -enoent; } node = (void*) mmap(null, header_size_b + bitmap_size_b + node_table_size_b , prot_read, map_shared, fd, 0); inicio = node; node = &(node[gfilebyblock + bitmap_block_size]); // busca el nodo sobre el cual se encuentre el nombre. node_name = &(node->fname[0]); (i = 0; ( (node->parent_dir_block != nodo_anterior) | (strcmp(nombre, (char*) node_name) != 0) | (node->state == 0)) & (i < gfilebytable) ; i++ ){ node = &(node[1]); node_name = &(node->fname[0]); } // cierra conexiones y libera memoria. free(start); free(start_super_path); if (munmap(inicio, header_size_b + bitmap_size_b + node_table_size_b) == -1) printf("error"); close(fd); if (i >= gfilebytable) return -1; return (i+1); }
the problem happens when call function with:
determinar_nodo("/otra carpetita/inside otra/inside otra otra :d/");
there many redundant steps in code because of debbuging, try step them over.
which matters me memory problem. notice @ beggining of declarations create start , start_super_path pointers not loose address free @ end of function. in addition, seems function breaks @ second call if it. means first "free" working properly, correspond call of determinar_nodo("/otra carpetita/")
, breaks @ free of call determinar_nodo("/otra carpetita/inside otra/")
.
debbuging on memory browser, notice adresses function trying free indeed correct, don't know going on. anyway, if free of those, got mentioned error.
thanks in advance help, , please forgive poor english.
you must have (char*) malloc(strlen(path)+1)
(+1 \0 terminating character).
otherwise, you'll have buffer overflow can corrupt heap.
related link: how malloc() , free() work?
Comments
Post a Comment