调用free()函数时的SIGTRAP

时间:2014-10-26 17:42:05

标签: c free dynamic-memory-allocation

我在尝试释放动态创建的数组时收到SIGTRAP信号,并且不知道原因。

我像这样分配数组:

int* visited = (int*) malloc( l.nodeCount * sizeof(int));

(l.nodeCount是一个整数。在程序实例中我得到这个错误,它被设置为12。)

当我尝试free(visited)时,我在调试器中得到了SIGTRAP信号。

整个功能就是这个:

int Graph_GetSmallestPathCount(AdjacencyList l, int destination){

//One path if destination is root
if(destination == 0) return 1;

if(l.nodeCount == 0)
    return 0;

Queue reading = Queue_NewQueue();
Queue storing = Queue_NewQueue();

/*Allocates visited array*/
int* visited = (int*) calloc( l.nodeCount, sizeof(int));

/*Visited array initialization*/
int i;
for(i = 0; i < l.nodeCount; i++)
    visited[i] = 0;

/*Marks root node and enqueues it*/
visited[0] = 1;
Queue_Enqueue(&reading, 0);

//While there are nodes to read
while(!Queue_IsEmpty(reading))
{

    //Dequeues a node
    int v = Queue_Dequeue(&reading);

    //Gets it's adjacency list
    List* currentList = AdjacencyList_GetAdjacentNodes(l, v);
    listCell* auxCell = currentList->head->next;

    //While there are nodes in it's adjacency list
    while(auxCell != NULL){

        //Enqueues it if it has not been visited
        if(visited[auxCell->data] == 0){
            Queue_Enqueue(&storing, auxCell->data);
        }

        //Adds to the paths to that node
        visited[auxCell->data] += visited[v];

        auxCell = auxCell->next;
    }

    //When the queue ends
    if(Queue_IsEmpty(reading)){

        //If the destination has been reached, return
        if(visited[destination] > 0){
            Queue_Destroy(&reading);
            Queue_Destroy(&storing);
            return visited[destination];
        }
        else{
            //Switch queues
            Queue_Destroy(&reading);

            reading = storing;
            storing = Queue_NewQueue();
        }
    }

}

//Destination has not been reached before end of algorithms. Deallocate everything and return 0
free(visited);
Queue_Destroy(&reading);
Queue_Destroy(&storing);

return 0;

}

对于缺少评论感到抱歉,我在运行中做了这个并且没有放入任何内容。对于printf超载,我把它们放在那里,同时试图查明问题。 编辑:我把它清理了一下。

奇怪的是,该程序适用于某些输入,而不适用于其他输入。

希望有人可以帮助我= D

3 个答案:

答案 0 :(得分:2)

我不能告诉你为什么你得到一个SIGTRAP,因为你没有发表一个最小的例子。

但是,我可以告诉你如何找出自己:

  1. 让您的程序可读。每行使用一条指令。 indent工具是您的朋友。当然,这不会修复错误,但它会让你更容易找到它。

  2. 不要malloc那样。无需转换malloc的返回值,无论如何使用calloc(l.nodeCount, sizeof (int));或类似内容更具可读性。

  3. SIGTRAP实际上意味着您遇到了断点指令。毫无疑问,实际发生的是你已经跳到了不是你的代码的东西,可能根本就不是代码,而是包含断点的二进制代码。为什么会这样?正常原因是内存损坏,尤其是堆栈损坏。我猜free()正在腐蚀它自己的堆栈。我猜这是因为你(某处)在你分配的内存之外写入内存。要对此进行测试,请使用malloc()/calloc()然后紧跟free()exit(0)来运行您的计划。如果可行,您知道问题是您正在做的事情。

  4. 我们无法分辨你在做什么,因为你没有(谢天谢地)发布完整的程序,但尝试在valgrind下运行它。当您获得超出范围的写入时,valgrind通常会将其提取出来。修复每个valgrind警告。这不能保证解决方案,但在我的经验中会找到95%的时间。

  5. 另请注意,return visited[destination];似乎在没有free() - visited的情况下退出该功能,因此内存泄漏。

答案 1 :(得分:0)

首先,不要像这样调用malloc()。 l.nodeCount * sizeof(int)可能会超过INT_MAX,您可能会遇到安全漏洞,或者如果您幸运,可能会发生崩溃。

相反,请使用calloc(l.nodeCount, sizeof(int))

如果程序内存不足以分配,您还应检查malloccalloc的返回值是否为NULL。

答案 2 :(得分:0)

找到答案。确实有可能在特定情况下,使用一个小于它应该的元素创建数组。我的坏。

感谢所有帮助过的人= D