R
Ram
Hi,
there are two instances where my code is crashing dute to
Segmentation fault when i tried to free memory.
Code 1:
while(..)
{
....
if((objects = (object_list * )calloc(1, sizeof(object_list))) ==
NULL)
return -1
tail = node_append(&head_file, tail_file, objects, sizeof
*objects);
//inside this function, i am using memcpy to copy the objects into
node.
free(objects);
//after returning form the above function, i did free(objects).. here
program giving segmentation fault after reaching last node.
}
case 2:
while(..)
{
char *file_pathname;
char *file_name = NULL;
if((file_name = (char *)malloc(some_size)) == NULL)
return -1;
file_pathname = get_name();
//do some operations on file path name & get only filename
....
strcpy(file_name, file_pathname); //segfault if no malloc for
file_name??
some_func(file_name); // inside the func, i will strcpy the
filename to some other string.
free(file_name); // now here sgnentation fault??
}
even, do i really need to malloc file_name in this case??
please bare and forgive me with my bad style of explanation? if still
not clear what i am doing please find complete code here-->
code.c
my_func(ss *t)
{
object_list *objects = NULL;
char *filepath_name = NULL;
char *filename = NULL;
int newObj = 0;
int id = 0;
node *head = NULL;
node *tail = NULL;
while ((i = read(t)) == 0)
{
filepath_name = get_pathname(t);
if((objects = (object_list * )calloc(1, sizeof
(object_list))) ==
NULL)
return -1;
newObj = obj_id++;
nObjects++;
objects->object_id= newObj;
//DO SOME MORE STUFF HERE
tail = node_append(&head, tail,
objects, sizeof *objects);
if (tail == NULL)
break;
if (some_logic)
{
info.first = head;
info.last = tail;
info.cmpfunc = cmp_dirPath;
found = list_search(&info, pathname);
if (found != NULL)
{
object_list *xx = found->data;
id = xx->object_id;
filename = strrchr
( filepath_name, '/')+1;
}
else
{
id = 1;
(void)strcpy(filename,
filepath_name);
}
}
write_header(newObj, id, filename);
free(filename); // very first in while loop giving
segfault
free(objects); // at the end of while loop list, i.e
after last node
giving segfault
}
node_free(head, free);
return (i == 1 ? 0 : -1);
}
Thank for your time !
-Ram
there are two instances where my code is crashing dute to
Segmentation fault when i tried to free memory.
Code 1:
while(..)
{
....
if((objects = (object_list * )calloc(1, sizeof(object_list))) ==
NULL)
return -1
tail = node_append(&head_file, tail_file, objects, sizeof
*objects);
//inside this function, i am using memcpy to copy the objects into
node.
free(objects);
//after returning form the above function, i did free(objects).. here
program giving segmentation fault after reaching last node.
}
case 2:
while(..)
{
char *file_pathname;
char *file_name = NULL;
if((file_name = (char *)malloc(some_size)) == NULL)
return -1;
file_pathname = get_name();
//do some operations on file path name & get only filename
....
strcpy(file_name, file_pathname); //segfault if no malloc for
file_name??
some_func(file_name); // inside the func, i will strcpy the
filename to some other string.
free(file_name); // now here sgnentation fault??
}
even, do i really need to malloc file_name in this case??
please bare and forgive me with my bad style of explanation? if still
not clear what i am doing please find complete code here-->
code.c
my_func(ss *t)
{
object_list *objects = NULL;
char *filepath_name = NULL;
char *filename = NULL;
int newObj = 0;
int id = 0;
node *head = NULL;
node *tail = NULL;
while ((i = read(t)) == 0)
{
filepath_name = get_pathname(t);
if((objects = (object_list * )calloc(1, sizeof
(object_list))) ==
NULL)
return -1;
newObj = obj_id++;
nObjects++;
objects->object_id= newObj;
//DO SOME MORE STUFF HERE
tail = node_append(&head, tail,
objects, sizeof *objects);
if (tail == NULL)
break;
if (some_logic)
{
info.first = head;
info.last = tail;
info.cmpfunc = cmp_dirPath;
found = list_search(&info, pathname);
if (found != NULL)
{
object_list *xx = found->data;
id = xx->object_id;
filename = strrchr
( filepath_name, '/')+1;
}
else
{
id = 1;
(void)strcpy(filename,
filepath_name);
}
}
write_header(newObj, id, filename);
free(filename); // very first in while loop giving
segfault
free(objects); // at the end of while loop list, i.e
after last node
giving segfault
}
node_free(head, free);
return (i == 1 ? 0 : -1);
}
Thank for your time !
-Ram