给出不同输入时出现分段错误

发布于 2024-08-18 15:17:44 字数 792 浏览 5 评论 0原文

我用 C++ 做了一些图像处理工作。为此,我使用 CImg.h 库,我觉得它对我的工作很有好处。

这是我编写的一小段代码,它只读取图像并显示它。

#include "../CImg.h"
#include "iostream"

using namespace std;
using namespace cimg_library;

int main(int argc,char**argv)
{
     CImg<unsigned char> img(argv[1]);
     img.display();  
     return 0;  
}

当我将 lena.pgm 作为输入时,它会显示图像。就好像我给出了一些其他图像,例如 ddnl.pgm ,我出现在同一目录中,我得到“分段错误”。

当我使用 gdb 运行代码时,我得到的输出如下:

程序收到信号 SIGSEGV,分段错误。
/lib/libc.so.6 中的 strlen () 中的 0x009823a3
缺少单独的调试信息,请使用: debuginfo-install glibc-2.9-2.i686 libX11-1.1.4-5.fc10.i386 libXau-1.0.4-1.fc10.i386 libXdmcp-1.0.2-6.fc10.i386 libgcc -4.3.2-7.i386 libstdc++-4.3.2-7.i386 libxcb-1.1.91-5.fc10.i386

有人可以告诉我问题是什么吗?以及如何解决。

谢谢大家

I do some image processing work in C++. For this i use CImg.h library which i feel is good for my work.

Here is small piece of code written by me which just reads an image and displays it.

#include "../CImg.h"
#include "iostream"

using namespace std;
using namespace cimg_library;

int main(int argc,char**argv)
{
     CImg<unsigned char> img(argv[1]);
     img.display();  
     return 0;  
}

When i give lena.pgm as input this code it displays the image. Where as if i give some other image, for example ddnl.pgm which i present in the same directory i get "Segmentation Fault".

When i ran the code using gdb i get the output as follows:

Program received signal SIGSEGV, Segmentation fault.
0x009823a3 in strlen () from /lib/libc.so.6
Missing separate debuginfos, use: debuginfo-install glibc-2.9-2.i686 libX11-1.1.4-5.fc10.i386 libXau-1.0.4-1.fc10.i386 libXdmcp-1.0.2-6.fc10.i386 libgcc-4.3.2-7.i386 libstdc++-4.3.2-7.i386 libxcb-1.1.91-5.fc10.i386

Can some one please tell me what the problem is? and how to solve it.

Thank you all

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

回忆凄美了谁 2024-08-25 15:17:44

当您尝试访问不允许访问的内存时,就会出现段错误。
所以请在代码中检查一下。

Segfault comes when you are trying to access memrory which you are not allowed to access.
So please check that out in the code.

奶气 2024-08-25 15:17:44

代码本身看起来很好。我可以建议一些继续调试的方法 -

  1. 尝试删除 display() 调用。问题还出现吗? (我假设是这样)。
  2. 尝试找出 CImg 代码中导致分段错误的 strlen() 位置(通过使用调试器)。这可能会给出额外的提示。
  3. 如果它在 PGM 文件处理中,则可能提供的 PGM 文件在某种程度上无效,并且库不执行错误检测 - 尝试在其他查看器中打开它,然后再次保存它(作为 PGM)。如果新的有效,比较两者可能会揭示一些东西。

一旦你掌握了更多信息,就可以说更多。

编辑 -

查看您提供的额外信息,并查阅代码本身,CImg 在尝试检查您正在打开的文件类型时似乎失败了。

相关的代码行是 -

if (!cimg::strcmp(ftype,"pnm")) load_pnm(filename);

这是第一次使用“ftype”,这使我得出的结论是它的值无效。

'ftype' 在上面几行被赋予了一个值 -

const char *const ftype = cimg::file_type(0,filename);

file_type() 函数本身尝试根据文件头猜测要打开的文件,可能是因为根据扩展名打开它失败了。只有一种明智的方法可以让它返回无效值,这会导致 strcmp() 失败 - 当它无法将文件识别为它熟悉的任何内容时,它会返回 NULL(实际上是 0)。

因此,我重申我的建议,即您尝试验证这确实是一个有效文件。我无法向您指出任何能够打开/保存 PGM 文件的工具,但我猜简单的 Google 搜索会有帮助。尝试打开该文件并将其重新另存为 PGM。

The code itself looks just fine. I can suggest some ways to go ahead with debugging -

  1. Try removing the display() call. Does the problem still occur? (I'd assume it does).
  2. Try finding out where in the CImg code is the strlen() that causes the segmentation fault (by using a debugger). This may give additional hints.
  3. If it is in the PGM file processing, maybe the provided PGM file is invalid in some way, and the library doesn't do error detection - try opening it in some other viewer, and saving it again (as PGM). If the new one works, comparing the two may reveal something.

Once you have more information, more can be said.

EDIT -

Looking at the extra information you provided, and consulting the code itself, it appears that CImg is failing when trying to check what kind of file you are opening.

The relevant line of code is -

if (!cimg::strcmp(ftype,"pnm")) load_pnm(filename);

This is the first time 'ftype' is used, which brings me to the conclusion that it has an invalid value.

'ftype' is being given a value just a few lines above -

const char *const ftype = cimg::file_type(0,filename);

The file_type() function itself tries to guess what file to open based on its header, probably because opening it based on the extension - failed. There is only one sane way for it to return an invalid value, which would later cause strcmp() to fail - when it fails to identify the file as anything it is familiar with, it returns NULL (0, actually).

So, I reiterate my suggestion that you try to verify that this is indeed a valid file. I can't point you at any tools that are capable of opening/saving PGM files, but I'm guessing a simple Google search would help. Try to open the file and re-save it as PGM.

泪眸﹌ 2024-08-25 15:17:44

分段错误的另一个“有趣的追踪”原因是库之间的编译器不匹配 - 这在使用 C++ 库时尤其普遍。

需要检查的事情是:

  1. 您是否使用与编译 CImg 库相同的编译器进行编译?
  2. 您使用相同的编译器标志吗?
  3. 是否有任何在编译库时设置但现在未设置的定义?

这些都曾以微妙的方式刺痛过我。

Another "fun to track down" cause of segmentation faults is compilier mismatches between libraries - this is especially prevalent when using C++ libraries.

Things to check are:

  1. Are you compiling with the same compiler as was used to compile the CImg library?
  2. Are you using the same compiler flags?
  3. Were there any defines that were set when compiling the library that you're not setting now?

Each of these has bitten me in subtle ways before.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文