2013-04-22 57 views
0

我写它的相关细节低于C程序:为什么会产生这一核心转储

void calculate(struct iso_matrix *iso_matrix) { 
     struct graphlet *graphlet = init_graphlet(GL_SIZE); 
     int *index_map = (int *)malloc(iso_matrix->n_rw_col); 
     //some other stuff. Working fine. 
     free(index_map); //line 90(for future references) 
} 

输出我在终点站下车:

*** glibc detected *** ./bin/exec: free(): invalid next size (fast):0x00000000023696f0 *** 
======= Backtrace: ========= 
/lib/x86_64-linux-gnu/libc.so.6(+0x7eb96)[0x2b3b5fc92b96] 
./bin/exec[0x403ff9] 
./bin/exec[0x4049eb] 
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xed)[0x2b3b5fc3576d] 
./bin/exec[0x400889] 
======= Memory map: ======== 
(not shown here) 

而且GDB回溯是:

#0 0x00007ffff7a51425 in raise() from /lib/x86_64-linux-gnu/libc.so.6 
#1 0x00007ffff7a54b8b in abort() from /lib/x86_64-linux-gnu/libc.so.6 
#2 0x00007ffff7a8f39e in ??() from /lib/x86_64-linux-gnu/libc.so.6 
#3 0x00007ffff7a99b96 in ??() from /lib/x86_64-linux-gnu/libc.so.6 
#4 0x0000000000403ff9 in calculate (iso_matrix=0x6084a0) at src/graphlet_iso_mat.c:90 
#5 0x00000000004049eb in main (argc=3, argv=0x7fffffffdef8) at src/main.c:70 

我无法理解为什么会发生这种情况或如何调试。任何帮助赞赏。

[编辑]完整的calculate功能:

void calculate(struct iso_matrix *iso_matrix) 
{ 
    printf("Calculate called\n"); 
    struct graphlet *graphlet = init_graphlet(GL_SIZE); 
    int *index_map = (int *)malloc(iso_matrix->n_rw_col); 
    struct graph *graph = init_graph(0, GL_SIZE); /*Small graph so prefered matrix representation.*/ 

    /*Initialize the list_head.*/ 
    if(!iso_matrix->unique) 
     iso_matrix->unique = init_listhead(); 

    for(int i=0; i<iso_matrix->n_rw_col; ++i) 
    { 
     graphlet_to_graph(graphlet, graph); 
     calc_heuristic(graph, 3); 

     /*check_unique() compares only between same type of graphs.*/ 
     index_map[i] = check_unique(iso_matrix->unique, graph); 
     if(index_map[i]==-1) 
     { 
      struct graph *cpy=init_graph(0, GL_SIZE); 
      cpy_graph(graph, cpy); 
      int *graphlet_no = (int *)malloc(sizeof(int)); 
      *graphlet_no = i; 
      struct container *container = (struct container *)malloc(sizeof(struct container)); 
      container->data = (void *)cpy; 
      container->id = (void *)graphlet_no; 
      struct list_node *list_node = init_listnode((void *)container); 
      add_to_list(list_node, iso_matrix->unique); 
     } 
     else 
     { 
      *(*((iso_matrix->iso_mat)+index_map[i])+i) = 1; 
     } 

     inc_graphlet(graphlet); 
     reset_graph(graph); 
    } 

    for(int i=0; i<iso_matrix->n_rw_col; ++i) 
    { 
     if(index_map[i]==-1) /*If same then continue.*/ 
      continue; 
     for(int j=0; j<iso_matrix->n_rw_col; ++j) 
      *(*((iso_matrix->iso_mat)+i)+j) = *(*((iso_matrix->iso_mat)+index_map[i])+j); 
    } 

    /*Destroying allocated memory.*/ 
    free(index_map); 
} 
+2

它告诉你,free()得到了一个指针,看起来不像它来自malloc()。可能有些东西在“//一些其他的东西”部分是用指针或覆盖它不应该覆盖的内存。仅仅因为代码有效,这并不意味着它是正确的。 :-) – 2013-04-22 00:25:00

+0

这可能是一个缓冲区溢出的地方。内存分配和释放通常检测堆结构中的损坏并发出警告和断言。如果可能的话,在'free'之前在调试器中断开并查看'index_map'周围的内存。您可能会覆盖分配给“index_map”的内存之前的空间中动态分配的缓冲区。 – 2013-04-22 00:20:03

+0

我检查了代码,它看起来不像我覆盖的东西。你能指出我可以从哪里读取这种形式的调试是使用GDB完成的。我也是GDB新手。另请参阅编辑的问题。 – 2013-04-22 00:32:52

回答

1

我下注,这个:

int *index_map = (int *)malloc(iso_matrix->n_rw_col); 

是为了指向的iso_matrix->n_rw_col整数的分配。您在字节计算中忘记了整数的大小:

int *index_map = malloc(iso_matrix->n_rw_col * sizeof(*index_map)); 

可能还有其他问题,但这显然是一个大问题。注意:我也删除了malloc()上的演员,你不应该用C代码。确保stdlib.h包含在此源文件顶部的#include列表中。

+0

我这么傻。你能告诉我关于“可能的其他问题” – 2013-04-22 00:49:18

+0

@AmanDeepGautam不是没有潜入代码。我从来不信任我没有彻底解析的代码,而且你有大量的函数调用谁是后端,我永远不会明智的。但是这个很明显。在** valgrind **下运行它以获得更高的信心。 – WhozCraig 2013-04-22 00:51:09

+0

@WhoCraig好的。我认为还有其他非常明显的问题。谢谢。 – 2013-04-22 00:53:07