Seg fault while allocating stream buffer

Hello,

I'm having trouble reading a bmp file header into a buffer that I've allocated for it. For the purposes of this assignment, I'm not allowed to use cstdlib buffer functions, so I'm (i) allocating the buffer myself, (ii) reading header data into it by decoding the binary byte by byte (for this I use a binarytools file which I know works), (iii) building my bmp structs out of buffer data and (iv) freeing the buffer memory afterward (then I'll go on to read the info header and then the image data). However, I'm getting a segfault in the first step of the process which I can't figure out.

Here's the function from the cpp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
void bitMap::loadBitMap(char *inFile)
{
	// open the input bmp for reading in binary mode
	FILE *in;
	in = fopen(inFile, "rb");
	
	// make sure the file opens properly
	if(inFile == NULL)
	{
		fputs("Error reading file", stderr);
		exit(1);
	}	

	// allocate a buffer to read in the file header (14 bytes)
	uint8 *fBufferPtr = new uint8[bmfh_size];
	fread(fBufferPtr, 1, bmfh_size, in); // read bmp header into the buffer
    const uint8 *streamBufferPtr = fBufferPtr; // copy the buffer pointer

	// build header struct out of buffer with binaryutils routines
	// first two bytes of fBuffer get decoded and stored as bmfh.bfType
	DecodeBinary_int16(&streamBufferPtr, &bmfh.bfType);
	// then do the check to ensure filetype == bmp
	if(bmfh.bfType != 19778) // hexidecimal address is 0x4D42
	{
		cout << "File is not a bitmap!" << endl;
		exit(2);
	}
	// next four bytes of fBuffer get decoded and stored as bmfh.bfSize
	DecodeBinary_int32(&streamBufferPtr, &bmfh.bfSize);
	// next two are bmfh.bfReserved1 (0)
	DecodeBinary_int16(&streamBufferPtr, &bmfh.bfReserved1);
	// next two are bmfh.bfReserved2 (0)
	DecodeBinary_int16(&streamBufferPtr, &bmfh.bfReserved2);
	// next four are bmfh.bfOffBits
	DecodeBinary_int32(&streamBufferPtr, &bmfh.bfOffBits);
	
	// now that the struct is built, let's clean up the buffer
	delete [] &fBufferPtr;
    
    // let's see if any of that was read in correctly by printing the struct
    cout << bmfh.bfType << endl;
	cout << bmfh.bfSize << endl;
	cout << bmfh.bfReserved1 << endl;
	cout << bmfh.bfReserved2 << endl;
	cout << bmfh.bfOffBits << endl;
}


Here's an expert of my header file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include "codec_binary.h"

using namespace std;

// general data declarations
const int bmfh_size = 14; // file header will always be 14 bytes in BMP files
const int bmih_size = 40; // info header will always be 40 bytes in BMP files

// bitmap header
struct bmpFileHeader
{
	int16 bfType; // file type
	int32 bfSize; // size in bytes of bmp
    int16 bfReserved1;
	int16 short bfReserved2;
	int32 bfOffBits;
};


Here's the valgrind diagnosis:

==22766== Invalid read of size 8
==22766== at 0x25FB8A: __fread (in /usr/lib/system/libsystem_c.dylib)
==22766== by 0x25FB33: fread (in /usr/lib/system/libsystem_c.dylib)
==22766== by 0x1000015D7: bitMap::loadBitMap(char*) (bitMap.cpp:41)
==22766== by 0x100001246: main (reader.cpp:18)
==22766== Address 0x68 is not stack'd, malloc'd or (recently) free'd

Can anyone make heads or tails of what I'm doing wrong?
This:
1
2
	// now that the struct is built, let's clean up the buffer
	delete [] &fBufferPtr;

should be:
1
2
	// now that the struct is built, let's clean up the buffer
	delete [] fBufferPtr;
Good point... that was something I changed temporarily just to test, but I've changed it back and the seg fault remains, so that's not the source of the issue.
I dunno what's going on. As bmfh_size is a constant and is only 14 bytes, can you just declare a char array instead of the new/delete stuff and see what happens?

Also, you don't check if the file was opened ok and you don't close it when done.
I'll give you that I don't close it (I'm not done with it, this is just the first of three read processes), but isn't this an adequate check to see if it's open?

1
2
3
4
5
6
	// make sure the file opens properly
	if(inFile == NULL)
	{
		fputs("Error reading file", stderr);
		exit(1);
	}


I can declare a char array, but an unsigned char should be no different than a uint8 (8 bit data type that holds a number) and I've been advised to use uint8 as char's can sometimes be allocated different amounts of memory on different machines and I am to be very explicit about the number of bytes.
inFile == NULL
inFile is the string, not the FILE pointer. 'in' is the FILE pointer.
The type of the prarameter for DecodeBinary_int16(&streamBufferPtr, ... is const uint8 **. That means you cannot manipulate the data, only the pointer to the data. Is this intended? What does DecodeBinary_... do?
That is intended, I don't want to manipulate anything in the buffer stream, only copy its contents into the structs that I'm building. The DecodeBinary functions are a bit beyond me, but I know they read in a certain number of bytes and parse the contained binary information into appropriate int values, etc.

I actually got my program working, I didn't realize that the DecodeBinary functions were reversing the bytes of my int's because I was working with little endian.

Thanks for all your help, guys.
Topic archived. No new replies allowed.