- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我在尝试释放动态创建的数组时收到 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
最佳答案
我不能告诉你为什么你得到一个 SIGTRAP
因为你还没有发布一个最小的例子。
不过,我可以告诉你如何自己找出答案:
使您的程序可读。每行使用一条指令。 indent
工具是您的好 helper 。当然,这不会修复错误,但会让您更容易找到它。
不要那样malloc
。不需要强制转换 malloc
的返回值,并且使用 calloc(l.nodeCount, sizeof (int));
或类似的方法无论如何都更具可读性。
SIGTRAP
实际上意味着您遇到了断点指令。毫无疑问,实际发生的事情是您跳转到了不是您的代码的地方,并且可能根本不是代码,但包含断点的二进制代码。为什么会这样?正常原因是内存损坏,尤其是堆栈损坏。我猜 free()
正在破坏它自己的堆栈。我猜这是因为你(在某处)写入了你分配的内存之外的内存。要对此进行测试,请使用 malloc()/calloc()
运行您的程序,紧接着是 free()
和 exit(0)
.如果可行,您就知道问题出在您正在做的事情之间。
我们无法知道您在这期间做了什么,因为您还没有(谢天谢地)发布完整的程序,但请尝试在 valgrind
下运行它。当您收到超出范围的写入时,valgrind
通常会拾取它。修复每个 valgrind
警告。这并不能保证找到解决方案,但根据我的经验,95% 的时间都会找到一个。
另请注意,return visited[destination];
似乎在没有 free()
-ing visited
的情况下退出函数,这因此是内存泄漏。
关于c - 调用 free() 函数时的 SIGTRAP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26575973/
所以这是我的问题,我创建了这个结构: typedef struct foo *fooPtr; struct foo { foo2Ptr node; foo3Ptr node; char *Random
我是 C 新手,无法解决这个问题。当我在文本文件上写入超过 27 个字符时,我会收到信号 SIGTRAP。跟踪/断点陷阱。我在网吧电脑windows 7上用Dev-C++编译。文本文件内容:Dinos
在运行时,我的程序经常因为 SIGTRAP 而停止。我知道,当编译器在程序中找到断点时,就会发生 SIGTRAP。但是我的代码中没有任何断点。 (为了确定,在执行之前,我清除了所有断点..)。 我正在
我正在尝试为 SIGTRAP 注册处理程序,该处理程序由在子级中调用 int3 引起,但它不起作用。将 SIGTRAP 更改为 SIGCHLD 即可。 #include #include #inc
我有麻烦了,每次我的函数调用“desenfileirar”时我都有一些断点。谁能帮我?我需要打印一个二维数组,表示一只 Ant 走过的路径。它需要从 (0,0) 开始并到达 (9,9)。我获得了成功,
背景 我有一个类似设置节点和边的网络。节点和边都需要是类,在本例中为 Node 或 Arc,as in this question .在我的实际设置中,我正在处理 Node 和 Arc 的相当多的子类
我正在尝试调试使用 gcc/g++ 构建的 Debian 程序(DEBUG=1 和 NDEBUG 未定义)。我正在使用第三方库,它也是为调试而构建的(DEBUG=1、NDEBUG 未定义,以及其他调试
我的一个 C++ OpenGL 程序中出现了一个奇怪的 SIGTRAP 错误。 首先,创建一个带有 new[] 运算符的浮点数组,如下所示: std::vector faces = readObjFi
我有一个多线程应用程序,它在我的所有测试机器上都非常稳定,并且对于我的几乎每一位用户来说似乎都是稳定的(基于没有崩溃投诉)。不过,对于一位好心的用户来说,该应用程序经常崩溃,他发送了崩溃报告。所有崩溃
如果有人可以花时间帮助我,我将非常感激!!! 我现在面对的是 我正在开发一个用于自动填充密码的应用程序,这意味着在网站登录期间单击键盘上方QuickType托架中的小键图标时,将触发我的应用程序。 然
我有一个关于释放为结构数组分配的内存的新手问题。这是代码: typedef struct { char code[6]; char name[31]; char cname[3
我试图释放一个表示 bmp 图像的三维指针数组,当它编译正常时,我在调试时在 gdb 中收到 SIGTRAP 信号。我的错误消息是 warning: HEAP[bmpsample.exe]: warn
这可能是一个有点晦涩或特定于项目的问题,但我的项目正在为遇到的每个异常抛出一个 SIGTRAP。我以前从未见过这个,现在每次我的代码出现问题时它都会抛出它并且它没有帮助我调试它。在我的另一个类似的类中
对于 test1.c ,我得到: 5 133 1 0 表示子进程先得到SIGTRAP (5), 原因 execl .最后三行表示子进程由于 SIGSTRAP 而终止。来自 parent 的信号。 //
我正在为 Windows 命令行编写一个基于控制台的小型应用程序。 我正在为两个数组 chi 和 chi_comp 分配内存,如下所示: /* allocating memory for chi */
我的 iOS 应用程序在测试中运行良好,但在启动时崩溃,并使用从 App Store 下载的发布版本。我已经尝试使用 Xcode 中的发布构建配置重新测试并且它没有崩溃,只是商店版本。我很确定它与应用
在极少数情况下,我们的一些用户会在我们的应用程序中遇到崩溃。它开始正常,但在他们从菜单中选择内容时崩溃后不久。那些看到它的人总能得到它,其他人都工作得很好。有人可以看到可以帮助我追踪它的东西吗? Ex
我正在运行我的应用程序运行良好,直到我从后台恢复或没有定位服务,应用程序会崩溃。 崩溃日志显示异常类型:EXC_CRASH (SIGTRAP) 有人知道如何调试吗? Exception Type:
我正在为一个STM32嵌入式系统调试这个软件。在其中一个函数中,我的程序不断遇到某种断点: SIGTRAP, Trace/breakpoint trap 但是,在 GDB 中,当我执行 info br
从 ptrace 的角度来看,我正在寻找一种与架构无关的方法来获取生成 SIGTRAP 的最后一次尝试的指令(或过去的指令)的指令指针(又名程序计数器)示踪剂。 一种依赖架构的方式是使用PTRACE_
我是一名优秀的程序员,十分优秀!