开发者

Segmentation fault when different input is given

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 sol开发者_运维知识库ve it.

Thank you all


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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜