- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在编写一段代码来检查文件是否存在。当用户传入一个只有名称(无斜杠)的文件参数时,我将使用 PATH 环境变量中的路径检查该文件是否存在。我在用 gcc 编译的 Ubuntu 上,一直在使用 gdb。我找到了导致 SIGABRT 的行,但不确定信息告诉我的是什么。这是我第一次使用 gdb。这是我的片段:
char* PATH = NULL;
PATH = (char*)getenv( "PATH" );
char* pathtoken = strtok( PATH, ":" );
int index = 0;
while ( pathtoken != NULL )
{
index++;
printf( "%s\n", pathtoken );
char* pathdir = (char*)malloc( sizeof(pathtoken) );
strcpy( pathdir, pathtoken );
GetFullPath( pathdir, filename );
inputstream = fopen( pathdir, "r" );
if ( inputstream != NULL)
{
free( pathdir );
break;
}
free( pathdir );
pathtoken = strtok( NULL, ":" );
}
char* GetFullPath( char* dirpath, char* filename )
{
//Append back slash
int len = strlen(dirpath);
dirpath[len] = '/';
dirpath[len+1] = '\0';
return strcat( dirpath, filename );
}
它发生在 fopen 行的第一次迭代中。这是我的 gdb 输出
Starting program: /home/seapoe/Documents/OsClass/CodePractice/a.out text1 /usr/lib/lightdm/lightdm
Breakpoint 1, main (argc=2, argv=0xbffff3a4) at xssh.c:87
87 GetFullPath( pathdir, filename );
(gdb) next
89 inputstream = fopen( pathdir, "r" );
(gdb) print pathdir
$5 = 0x804c008 "/usr/lib/lightdm/lightdm/text1"
(gdb) print pathdir[strlen(pathdir)]
$6 = 0 '\000'
(gdb) print pathdir[strlen(pathdir)+1]
$7 = 0 '\000'
(gdb) print pathdir[strlen(pathdir)+2]
$8 = 0 '\000'
(gdb) print pathdir[strlen(pathdir)-1]
$9 = 49 '1'
(gdb) next
* glibc detected /home/seapoe/Documents/OsClass/CodePractice/a.out: free(): invalid next size (normal): 0x0804c018 **
======= Backtrace: =========
/lib/i386-linux-gnu/libc.so.6(+0x75ee2)[0xb7e97ee2]
/lib/i386-linux-gnu/libc.so.6(+0x65db5)[0xb7e87db5]
/lib/i386-linux-gnu/libc.so.6(fopen+0x2b)[0xb7e87deb]
/home/seapoe/Documents/OsClass/CodePractice/a.out[0x8048a77]
/lib/i386-linux-gnu/libc.so.6(__libc_start_main+0xf3)[0xb7e3b4d3]
/home/seapoe/Documents/OsClass/CodePractice/a.out[0x8048771]
======= Memory map: ========
08048000-0804a000 r-xp 00000000 08:01 536263 /home/seapoe/Documents/OsClass /CodePractice/a.out
0804a000-0804b000 r--p 00001000 08:01 536263 /home/seapoe/Documents/OsClass/CodePractice/a.out
0804b000-0804c000 rw-p 00002000 08:01 536263 /home/seapoe/Documents/OsClass/CodePractice/a.out
0804c000-0806d000 rw-p 00000000 00:00 0 [heap]
b7df3000-b7e0f000 r-xp 00000000 08:01 394136 /lib/i386-linux-gnu/libgcc_s.so.1
b7e0f000-b7e10000 r--p 0001b000 08:01 394136 /lib/i386-linux-gnu/libgcc_s.so.1
b7e10000-b7e11000 rw-p 0001c000 08:01 394136 /lib/i386-linux-gnu/libgcc_s.so.1
b7e21000-b7e22000 rw-p 00000000 00:00 0
b7e22000-b7fc5000 r-xp 00000000 08:01 394115 /lib/i386-linux-gnu/libc-2.15.so
b7fc5000-b7fc7000 r--p 001a3000 08:01 394115 /lib/i386-linux-gnu/libc-2.15.so
b7fc7000-b7fc8000 rw-p 001a5000 08:01 394115 /lib/i386-linux-gnu/libc-2.15.so
b7fc8000-b7fcb000 rw-p 00000000 00:00 0
b7fd9000-b7fdd000 rw-p 00000000 00:00 0
b7fdd000-b7fde000 r-xp 00000000 00:00 0 [vdso]
b7fde000-b7ffe000 r-xp 00000000 08:01 394095 /lib/i386-linux-gnu/ld-2.15.so
b7ffe000-b7fff000 r--p 0001f000 08:01 394095 /lib/i386-linux-gnu/ld-2.15.so
b7fff000-b8000000 rw-p 00020000 08:01 394095 /lib/i386-linux-gnu/ld-2.15.so
bffdf000-c0000000 rw-p 00000000 00:00 0 [stack]
Program received signal SIGABRT, Aborted.
0xb7fdd424 in __kernel_vsyscall ()
在调用 GetFullPath 之后,您可以看到在 fopen 调用之前返回了一个有效路径
最佳答案
char* pathdir = (char*)malloc( sizeof(pathtoken) );
错了。 sizeof(pathtoken) 将为您提供系统指针的大小(4 或 8)。你想要
pathdir = strdup(pathtoken)
这也是错误的,因为您正在尝试附加到它。只需将 pathdir 设为 MAX_PATH(或 PATH_MAX,具体取决于您的操作系统)长度的字符数组。
关于c - SIGABRT 在 fopen 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19060729/
我的代码在使用 malloc 时遇到问题。直到一个小时前它一直运行良好。它导致这一行 temp2 = (Temp*)malloc(sizeof(Temp));
我的应用程序运行完美,没有任何问题。但是当我尝试调试它时,android studio 卡住了。所以我收到消息“等待调试器”,然后该消息消失,接下来我在模拟器中只看到黑屏。 我也收到了消息 SIGNA
我用 node-js 和 fluent-ffmpeg 编写了一个 node-js api: 'use strict'; require('babel-register'); const path =
我只是在过去一直工作的模拟器上运行我的代码。我真正为启动代码所做的唯一一件事就是设置用户默认值 NSUserDefaults *defaults = [NSUserDefaults standard
我遇到应用程序每次运行时都会在模拟器上崩溃。由于某种原因,他们收到一个中止信号(见附件截图)。最近才开始用,不知道是不是和10.8.4升级有关。 我刚刚基于“单一 View 应用程序”模板创建了一个新
我的应用程序中存在一个错误,使我发疯!尝试隐藏navigationController工具栏后,主线程会收到SIGABRT消息。 [self.navigationController setToolb
我无法理解以下崩溃,因为它是随机发生的,并且我怀疑可能是多线程问题,但是这里是: Incident Identifier: 0BE956AB-228A-4B1B-8A3D-A99A481F7F3F C
我最近设置了 Crashlytics 以从我的 iOS 应用程序接收崩溃日志,并且我不断收到 SIGABRT 崩溃日志,但我找不到它的来源。 如果你能帮我一点,这是崩溃日志: Exception Ty
我正在尝试通过指针来理解这项工作。所以我编写了一个测试程序,其中通过删除分隔点将名称分成标签。每个标签都表示为长度/数据对,如下所示:google.ru 表示为“x\06googlex\02ru”当我
我正在研究教授给我们的一些旧课本,为即将到来的考试做准备,我遇到了这个问题。 我的任务是从结构如下的文本文件中读取信息: [十进制数字],[罗马数字(字符串)],[o 或 u(优化或未优化的罗马数字)
#toggle the string #include int main() { char S[100],ch; int i=0; gets(S); while((S[i]!
我正在开展一个学校项目,我需要从文件中获取矩阵的信息(高度、宽度、单元格状态)。像这样的事情: 30 40 /*height and width*/ 3 /*nr of lines
我学习 Swift 一段时间了,SIGABRT 信号随机出现了好几次。我尝试过一些在线教程,但似乎并不总是有效。 这次我试图用两个 View Controller 设置一个待办事项列表。一个有一个表格
我在我的项目中添加了第二个目标,以便能够对多个应用程序使用相同的 Xcode 项目(基本应用程序的风格略有变化)。我重命名了目标,更改了方案以匹配名称,还重命名了第二个 info.plist。现在,当
我试图运行一个简单的代码,它编译但当我尝试运行它时我得到(核心转储)错误。于是用gdb查看错误是什么。 代码: #include #include #include void gerar() {
我为这个问题苦苦挣扎了 2 天。我有一个解决方法,但我想了解更多会发生什么。让我们开始吧。我有一个非常原始的异常类,它保存一条错误消息作为指向字符数组的指针(我知道 std::string 的利润)。
NSString *aName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
我正在尝试从框架动态加载类,但应用程序因转换发送 SIGABRT 信号而崩溃: let newClassType = NSClassFromString("MyFramework.CustomClas
当我运行我的应用程序时, View Controller 中会调用一个函数,当发生这种情况时,我会收到信号 SIGABRT。 如何解决这个问题? 功能: func setRootViewControl
我正在尝试保存 UISwitch 结果并使用它们来填充 Parse.com 推送通知的“ channel ”。我遵循了解析指南,但每次我尝试单击保存开关值的保存按钮时,我都会收到一个 SIGABRT。
我是一名优秀的程序员,十分优秀!