Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

SIGSEGV on fclose()

113 views
Skip to first unread message

Harald Grossauer

unread,
Dec 18, 2002, 11:16:03 AM12/18/02
to
Hi,

I have a strange problem getting a SIGSEGV from a fclose(). It does not
happen for every set of data but I have a set of data where it happens
always.

The function (code see below) should write an image given as a red[],
green[] and blue[] array into a bitmap file. The output is the following:

<code>
Starting writeimg()
Before fclose()
MPI process rank 0 (n0, p27563) caught a SIGSEGV.
----------------------------------------------------------------------------
-

One of the processes started by mpirun has exited with a nonzero exit
code. This typically indicates that the process finished in error.
If your process did not finish in error, be sure to include a "return
0" or "exit(0)" in your C code before exiting the application.

PID 27563 failed on node n0 with exit status 1.
----------------------------------------------------------------------------
-
</code>

As you can see it is an MPI app. I have a "master" process which does all
the file I/O, spreads the data between several "child" processes, collects
the processed data and writes it back to the file. The strange thing is,
that if I replace a line "vabs = vx*vx+vy*vy;" in the processing routine of
the child processes with the correct "vabs = sqrt(vx*vx+vy*vy);" the SIGSEGV
does not occur!

Ok, here is the code:

int writeimg(const unsigned char* red, const unsigned char* green, const
unsigned char* blue, const int width, const int height, const char* name)
{
int i,j;
char bitmapHeader[60];
*(short*)&bitmapHeader[0] = 0x4d42; // BM
*(int*)&bitmapHeader[2] = 0; // filesize in byte, may be 0 for uncompressed
*(int*)&bitmapHeader[6] = 0; // Reserved
*(int*)&bitmapHeader[10] = 56; // Offset of image data
*(int*)&bitmapHeader[14] = 40; // size of bitmapinfoheader structure
*(int*)&bitmapHeader[18] = width; // width
*(int*)&bitmapHeader[22] = height; // height
*(short*)&bitmapHeader[26] = 1; // planes
*(short*)&bitmapHeader[28] = 24; // bitcount
*(int*)&bitmapHeader[30] = 0; // compression
*(int*)&bitmapHeader[34] = 0; // SizeImage (may be set to zero for
uncompressed images)
*(int*)&bitmapHeader[38] = 0;
*(int*)&bitmapHeader[42] = 0;
*(int*)&bitmapHeader[46] = 0;
*(int*)&bitmapHeader[50] = 0;

FILE* file;
if(!(file = fopen(name, "w"))) {cout << name << ": Error opening file\n";
return 1;}
if(fwrite(bitmapHeader, 54, 1, file) != 1) {cout << name << ": Error
writing bitmap header\n"; fclose(file); return 1;}
int noPadBytes = (4-(width*3)%4)%4;
int resr = 0; int resg = 0; int resb = 0;
for ( j = 0; j < height; j++)
{
for(i = 0; i < width && resr!=EOF && resg!=EOF && resb!=EOF ; i++)
{
resb = fputc(blue[j*width+i], file);
resg = fputc(green[j*width+i], file);
resr = fputc(red[j*width+i], file);
}
if(resr==EOF || resg==EOF || resb==EOF){cout << name << ": Error writing
image data\n"; fclose(file); return 1;}
if(noPadBytes)
{
for(i=0;i<noPadBytes;i++)
{
fputc(0, file);
}
}
fflush(file);
}
cout << "Before fclose()\n";
fclose(file);
cout << "After fclose()\n";
return 0;
} // end writeimg()


Kevin Easton

unread,
Dec 18, 2002, 6:29:36 PM12/18/02
to
Harald Grossauer <csaa...@uibk.ac.at> wrote:
> Hi,
>
> I have a strange problem getting a SIGSEGV from a fclose(). It does not
> happen for every set of data but I have a set of data where it happens
> always.
>
> The function (code see below) should write an image given as a red[],
> green[] and blue[] array into a bitmap file. The output is the following:

The code you pasted looks fine to me. I suspect you have a bug
elsewhere in your code that is corrupting the heap, causing a memory
allocation or (more likely) free() call by fclose() to crash.

Try running your code with Valgrind.

- Kevin.

Kasper Dupont

unread,
Dec 19, 2002, 1:21:25 AM12/19/02
to
Harald Grossauer wrote:
>
> Hi,
>
> I have a strange problem getting a SIGSEGV from a fclose(). It does not
> happen for every set of data but I have a set of data where it happens
> always.

Probably related to this:
http://www.daimi.au.dk/~kasperd/comp.os.linux.development.faq.html#SIGSEGV

--
Kasper Dupont -- der bruger for meget tid på usenet.
For sending spam use mailto:aaa...@daimi.au.dk
Hvem er fjenden i Aalborg?

0 new messages