malloc() free() 에 관한 이해하기 힘든 버그입니다..
#include "list_file.h"
#include "common.h"
#define MAX_PATH 2048
list_file* get_list_file( char *dirname ,int *max_count) {
printf("start get_list_file()1\n");
DIR *dir_ptr; /* the directory */
struct dirent *direntp; /* each entry */
printf("dirpath %s\n",dirname);
strncpy(pub_present_path,dirname,strlen(dirname));
list_file *p=NULL;
list_file *head=NULL;
list_file tail;
printf("start get_list_file()3\n");
head=(list_file*)malloc(sizeof(list_file));
printf("::: Malloc :::\n");
printf("start get_list_file()4\n");
head->Next=NULL;
tail.Next=NULL;
printf("list_file() check\n");
if ( ( dir_ptr = opendir( dirname ) ) == NULL ) {
fprintf(stderr,"ls1: cannot open %s\n", dirname);
}
else {
while ( ( direntp = readdir( dir_ptr ) ) != NULL ) {
dostat(dirname, direntp->d_name ,head,&tail);
}
}
p=head;
int i=0;
printf("list_file() while before\n");
while(p=p->Next) {
p->data.count=++i;
}
*max_count=i;
closedir(dir_ptr);
return head;
}
void dostat(char *dirname, char *filename ,list_file *head,list_file *tail) {
struct stat info;
list_file *node=(list_file *)malloc(sizeof(list_file));
char path[strlen(dirname)+strlen(filename)+2];
memset(path,0x0,sizeof(path));
snprintf(path,(strlen(dirname)+strlen(filename))+2,"%s/%s",dirname,filename);
if ( stat(path, &info) == -1 ) {
perror( filename ); /* say why */
}
else {
if(strcmp(filename,".")==0 || strcmp(filename,"..")==0) {
;
}
else {
show_file_info(path, &info ,head,tail,filename);
}
}
}
void show_file_info( char *path, struct stat *info_p,list_file *head,list_file *tail,char *filename ) {
struct tm *tm_ptr;
char buffer[9];
list_file *t;
tm_ptr=localtime(&info_p->st_ctime);
strftime(buffer,9, "%y/%m/%d", tm_ptr);
list_file* node=(list_file*)malloc(sizeof(list_file));
printf("::: Malloc() ::: %d\n",sizeof(list_file));
node->Next=NULL;
node->data.ori_date=info_p->st_ctime;
node->data.ori_size = 0;
if ( S_ISDIR(info_p->st_mode) ) node->data.isdir=1;
else node->data.isdir=0;
memset(node->data.filename,0x0,256);
strncpy(node->data.filename,filename,strlen(filename));
printf("읽음 %s ,f %d,p %d\n",node->data.filename,strlen(filename),strlen(node->data.filename));
memset(node->data.date,0x0,9);
strncpy(node->data.date,buffer,9);
node->data.date[strlen(node->data.date)] = '\0';
strncpy(node->data.initial,filename,50-3);
node->data.initial[41]='.';
node->data.initial[42]='.';
node->data.initial[43]='\0';
if(S_ISDIR(info_p->st_mode)) {
node->data.size=0;
strncpy(node->data.unit," ",2);
}
else {
if(((long)info_p->st_size) > 1000000) {
strncpy(node->data.unit,"MB",2);
node->data.size=(float)((long)info_p->st_size) / 1000000;
node->data.unit[2]='\0';
node->data.ori_size = (long)info_p->st_size;
}
else {
node->data.size=(float)((long)info_p->st_size) / 1000;
strncpy(node->data.unit,"KB",2);
node->data.unit[2]='\0';
node->data.ori_size = (long)info_p->st_size;
}
}
printf("%s\n",node->data.date);
list_file *p;
p=head;
while(p->Next!=NULL) {
printf("add node\n");
t=p->Next;
if(t->data.ori_date < node->data.ori_date) {
t=p->Next;
p->Next=node;
node->Next=t;
break;
}
p=p->Next;
}
p->Next=node;
}
void delete_all(list_file *head,int max_count) {
list_file *p=NULL;
list_file *t=NULL;
p=head;
while((p->Next)!=NULL) {
t=p->Next;
free(p);
p=t;
printf("::: free()\n");
}
printf("head free()\n");
free(head);
}
파일,폴더가 0개 또는 2개 이상일때
메세지임
recv_message ; ㅅ
start message_process()
present path : /home/msghard/cho_hyunho@hotmail.com
number is : 0
:: /home/msghard/cho_hyunho@hotmail.com
::: free()
::: free()
head free()
start get_list_file()1
dirpath /home/msghard/cho_hyunho@hotmail.com
start get_list_file()3
::: Malloc :::
start get_list_file()4
list_file() check
::: Malloc() ::: 600
읽음 1 ,f 1,p 1
05/02/17
::: Malloc() ::: 600
읽음 훗 ,f 2,p 2
05/02/17
add node
list_file() while before
폴더,폴더가 1 개만 있었을때 읽음
recv_message ; ㅅ
start message_process()
present path : /home/msghard/cho_hyunho@hotmail.com
number is : 0
:: /home/msghard/cho_hyunho@hotmail.com
::: free()
head free()
start get_list_file()1
dirpath /home/msghard/cho_hyunho@hotmail.com
start get_list_file()3
이상하게 파일이 1개일경우에만 문제가 생기는데 이거때매 계속 헤메고 있습니다.. 당최.. 잘못됀곳이 어디인가요..[/code]


댓글 달기