-1

im having a segmentation fault error testing a client-server file transfer. I have this code on the downloader side (this is where i think the problem is):

else if (info_socket[fds[i].fd] == RECIBIENDO) {
                close_conn = FALSE;
                buffer = (char*) malloc(1500);
                rc = recv(fds[i].fd, buffer, 1500, 0);
                if (rc < 0) {
                    if (errno != EWOULDBLOCK) {
                        perror("  recv() failed");
                        close_conn = TRUE;
                        map<int, info_trans*>::iterator it = descargas.find(fds[i].fd);
                        descargas.erase(it);
                    }
                } else if (rc == 0) {
                    close_conn = TRUE;
                    map<int, info_trans*>::iterator it = descargas.find(fds[i].fd);
                    descargas.erase(it);
                } else {
                    arch = fopen((directorio + descargas[fds[i].fd]->nombre_archivo).c_str(), "ab");
                    printf("%s -- %d",(directorio + descargas[fds[i].fd]->nombre_archivo).c_str(),fds[i].fd);
                    totalEscrito = fwrite(buffer, 1, rc, arch);
                    descargas[fds[i].fd]->bytes_descargados = descargas[fds[i].fd]->bytes_descargados + totalEscrito;
                    fclose(arch);
                    file_descript = open((directorio + descargas[fds[i].fd]->nombre_archivo).c_str(), O_RDONLY);
                    file_size = get_size_by_fd(file_descript);
                    file_buffer = (char*) mmap(0, file_size, PROT_READ, MAP_SHARED, file_descript, 0);
                    MD5((unsigned char*) file_buffer, file_size, result);
                    mdString = (char*) malloc(33);
                    for (int r = 0; r < 16; r++)
                        sprintf(&mdString[r * 2], "%02x", (unsigned int) result[r]);
                    if (strcmp(mdString, (descargas[fds[i].fd]->md5).c_str()) == 0) {
                        close_conn = TRUE;
                        map<int, info_trans*>::iterator it = descargas.find(fds[i].fd);
                        descargas.erase(it);
                    }
                    free(mdString);
                   free(file_buffer);
                    close(file_descript);
                }

The Server is on a similar loop sending the data. The strange part is that if i run this on my laptop it works, but at the school pc's virtual machine throws the Segmentation fault. If there is a good way to debug the memory using VMware Player, please tell me too. Thanks you, and sorry for my english level.

4

1 回答 1

1

free(file_buffer);是错的。我猜你的意思是munmap(file_buffer, file_size);

于 2013-09-21T15:23:20.010 回答