wordexp 后跟 strcpy = EXC_BAD_ACCESS +共享库应用加载规则全部
言下之意就是内存问题。我对这些有静态分配:
char akdir[400];
char homedir[400];
这在第一个 strcpy() 上崩溃:
void setuplibfoo()
{
long ii;
double x;
wordexp_t result;
// This obtains the user's home directory
// --------------------------------------
homedir[0]=0; // in case wordexp fails
switch (wordexp("~/",&result,0))
{
case 0: // Successful. We'll fall into deallocate when done.
{
strcpy(homedir,result.we_wordv[0]); // <<--- CRASH!
strcpy(akdir,homedir);
strcat(akdir,"ak-plugins/");
vs_status(akdir);
}
case WRDE_NOSPACE: // If the error was WRDE_NOSPACE, then
{ // perhaps part of the result was allocated.
wordfree (&result);
}
default: // all other errors do not require deallocation
{
break;
}
}
...附加代码被剪切..在崩溃时不会到达那里。
这是在我编写的共享库中,该库链接到我的应用程序,也是我编写的内容。在这种情况下,它不会走得太远,但如果它开始了,那就没问题了。
...我已经多次阅读了 wordexp 文档;他们说他们分配新对象,所以你只需设置该类型并使用地址调用它们。开关错误模型来自 wordexp 文档:
http: //www.gnu.org/s/libc/manual/html_mono/libc.html#Wordexp-Example
它并不总是崩溃。只是有时,而且略低于 10.6。 我从来没有在 10.5 下
使用 XCode 3.1.1 构建调试模式,在 OSX 10.5.8 下它似乎运行正常,我没有看到崩溃 - 在 10.6 下,它有时会崩溃。但总是有同样的例外,而且总是在同一个地方。
谷歌认为,这实际上意味着,不知何故,现在分配内存还为时过早。但我能找到的所有实例都是程序员的内存错误。溢出等。而且我找不到任何有关何时安全分配内存的文档。
现在,扩展的路径接近 400 个字符。就是这个(它完成了):
/Users/flake/ak-plugins/
和这个:
/Users/flake/
...如果没有。
strcpy... 将第二个参数复制到第一个参数。他们的归我的。它有效! 10.5以下。 :/
那么 wordexp 坏了吗? 10.6坏了吗?我疯了吗?
这是调试器的输出:
0x00013446 <+0049> call 0xc98da <dyld_stub_wordexp>
0x0001344b <+0054> test %eax,%eax
0x0001344d <+0056> je 0x13454 <setuplibfoo+63>
0x0001344f <+0058> jmp 0x134da <setuplibfoo+197>
0x00013454 <+0063> mov -0x1c(%ebp),%eax
0x00013457 <+0066> mov (%eax),%eax
0x00013459 <+0068> mov %eax,0x4(%esp)
0x0001345d <+0072> lea 0xb6cc2(%ebx),%eax
0x00013463 <+0078> mov (%eax),%eax
0x00013465 <+0080> mov %eax,(%esp)
0x00013468 <+0083> call 0xc9898 <dyld_stub_strcpy>
0x0001346d <+0088> lea 0xb6cc2(%ebx),%eax <<--CRASH!
The implication is a memory problem. I have static allocations for these:
char akdir[400];
char homedir[400];
This crashes on the first strcpy():
void setuplibfoo()
{
long ii;
double x;
wordexp_t result;
// This obtains the user's home directory
// --------------------------------------
homedir[0]=0; // in case wordexp fails
switch (wordexp("~/",&result,0))
{
case 0: // Successful. We'll fall into deallocate when done.
{
strcpy(homedir,result.we_wordv[0]); // <<--- CRASH!
strcpy(akdir,homedir);
strcat(akdir,"ak-plugins/");
vs_status(akdir);
}
case WRDE_NOSPACE: // If the error was WRDE_NOSPACE, then
{ // perhaps part of the result was allocated.
wordfree (&result);
}
default: // all other errors do not require deallocation
{
break;
}
}
...additional code clipped.. doesn't get there on crash.
This is in a shared library I've written that is linked to my application, also something I've written. In this case, it doesn't get very far, although if it starts, it's fine.
...I've read the wordexp docs several times; they say they allocate new objects, so you just set up that type and call them with the address. The switch error model is right from the wordexp docs:
http://www.gnu.org/s/libc/manual/html_mono/libc.html#Wordexp-Example
It doesn't always crash. Just sometimes, and just under 10.6. Never under 10.5
I'm building debug mode with XCode 3.1.1, under OSX 10.5.8 it seems to run ok, I've not seen a crash -- under 10.6, it crashes... sometimes. But always with that same exception, and always in the same place.
The Google has it that this actually means, somehow, that it's too soon to allocate memory. But all the instances I could find were memory errors on the part of the programmer. Overruns, etc. And I can't find any docs on when it IS safe to allocate memory.
Now, the path that expands there is nowhere near 400 characters. it's this (it it completes):
/Users/flake/ak-plugins/
and this:
/Users/flake/
...if it doesn't.
the strcpy... copies 2nd param to first. Theirs to mine. And it works! under 10.5. :/
So is wordexp broke? Is 10.6 broke? Am I cRaZy?
Here's the debugger output:
0x00013446 <+0049> call 0xc98da <dyld_stub_wordexp>
0x0001344b <+0054> test %eax,%eax
0x0001344d <+0056> je 0x13454 <setuplibfoo+63>
0x0001344f <+0058> jmp 0x134da <setuplibfoo+197>
0x00013454 <+0063> mov -0x1c(%ebp),%eax
0x00013457 <+0066> mov (%eax),%eax
0x00013459 <+0068> mov %eax,0x4(%esp)
0x0001345d <+0072> lea 0xb6cc2(%ebx),%eax
0x00013463 <+0078> mov (%eax),%eax
0x00013465 <+0080> mov %eax,(%esp)
0x00013468 <+0083> call 0xc9898 <dyld_stub_strcpy>
0x0001346d <+0088> lea 0xb6cc2(%ebx),%eax <<--CRASH!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在尝试之前尝试将 homedir 设置为 0,并将“结果”wordexp_t 结构设置为零。我最近使用了它,它在 Mac 和 Linux 上都运行良好。我想你指的是 10.6 Mac?
Try memsetting homedir to 0 and the "result" wordexp_t structure to zeroes before trying. I recently used it and it works well on both Mac and Linux. By 10.6 I suppose you mean Mac?