Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I give the path of image fist time(c:\b.jpeg) and call ReadImage() it works but
when I call ReadImage() second time (c:\banner3.jpeg) it gives debug assertion failed.
Actully when I read the szFileName on second time call it shows bad pointer.
How to solve it?
C++
BITMAPINFOHEADER* ReadImage(char* szFileName)
{
	size_t nSize = 0;
	size_t nRead = 0;
	size_t nHeaderSize = 14;
	BITMAPINFOHEADER* pBitmap = NULL;
	// try to open the file.
	FILE* fp=fopen(szFileName, "rb");
	if (fp==NULL)
	{
		printf("%s: failed to open with errno %d.\n", szFileName, errno);
		return NULL;
	}

	// get the size if the bitmap
	fseek(fp, 0, SEEK_END);
	nSize = ftell(fp);
	fseek(fp, (long)nHeaderSize, SEEK_SET ); // really dirty skip the bitmap file header.
	nSize -= nHeaderSize; // adjust the size to fit the mem size

	// get the memory for the bitmap.
	pBitmap = (BITMAPINFOHEADER*)malloc(nSize);
	if (pBitmap==NULL)
	{
		printf("%s: failed to alloc memory.\n", szFileName);
		fclose(fp);
		return NULL;
	}

	// read data from disk
	nRead = fread(pBitmap, 1, nSize, fp);
	if (nRead!=nSize)
	{
		free(pBitmap);
		printf("%s: failed to read image.\n", szFileName);
		fclose(fp);
		return NULL;
	}
	
	fclose(fp);
		return pBitmap;
}


void main( int argc, char *argv[])
{
	DrmErrorCode_t nRetCode;
	FC3Template_t hTemplate1, hTemplate2;
	FC3Encode_t hEncoder;
	FC3Match_t hMatcher;
	double dScore;
	char image1[]={'a'};
	char image2[]={'h'};

scanf("%s",&image1);

	// Checking the arguments
	if (argc!=1)
	{
		printf("Usage: %s Image1.bmp, Image2.bmp\n", argv[0]);
		//return 1;
	}

	// Loading the image for quality check
	BITMAPINFOHEADER* pBitmap1 = NULL;
	BITMAPINFOHEADER* pBitmap2 = NULL;
	pBitmap1 = ReadImage(argv[0]);
	//pBitmap1 = ReadImage(argv[1]);
	if (pBitmap1==NULL)
	{
		printf("Error while reading Image1\n");
		//return 2;
	}

	scanf("%s",&image2);
pBitmap2 = ReadImage(argv[1]);
Posted
Updated 21-Jun-12 18:58pm
v3

Try this :) :
C++
// free((void*)szFileName);
 
Share this answer
 
Comments
Member 7909353 21-Jun-12 10:16am    
when I use free() function it gives following message
Windows has triggered a breakpoint in Tutorial.exe.
This may be due to a corruption of the heap, which indicates a bug in Tutorial.exe or any of the DLLs it has loaded.
This may also be due to the user pressing F12 while Tutorial.exe has focus.
The output window may have more diagnostic information.
Eugen Podsypalnikov 21-Jun-12 10:18am    
Please do not try to call it (free(..)), just comment it out :)
C++
BITMAPINFOHEADER* ReadImage(char* szFileName)
{
	size_t nSize = 0;
	size_t nRead = 0;
	size_t nHeaderSize = 14;
	BITMAPINFOHEADER* pBitmap = NULL;
	// try to open the file.
	FILE* fp=fopen(szFileName, "rb");
	if (fp==NULL)
	{
		printf("%s: failed to open with errno %d.\n", szFileName, errno);
		return NULL;
	}
 
	// get the size if the bitmap
	fseek(fp, 0, SEEK_END);
	nSize = ftell(fp);
	fseek(fp, (long)nHeaderSize, SEEK_SET ); // really dirty skip the bitmap file header.
	nSize -= nHeaderSize; // adjust the size to fit the mem size

	// get the memory for the bitmap.
	pBitmap = (BITMAPINFOHEADER*)malloc(nSize);
	if (pBitmap==NULL)
	{
		printf("%s: failed to alloc memory.\n", szFileName);
		fclose(fp);
		return NULL;
	}
 
	// read data from disk
	nRead = fread(pBitmap, 1, nSize, fp);
	if (nRead!=nSize)
	{
		free(pBitmap);
		printf("%s: failed to read image.\n", szFileName);
		fclose(fp);
		return NULL;
	}
	
	fclose(fp);
        // You can't free this parameter, because you use argv array item here
	//free((void*)szFileName); 
	return pBitmap;
}

void main( int argc, char *argv[])
{
	DrmErrorCode_t nRetCode;
	FC3Template_t hTemplate1, hTemplate2;
	FC3Encode_t hEncoder;
	FC3Match_t hMatcher;
	double dScore;
	char image1[]={'a'};
	char image2[]={'h'};
 
        scanf("%s",&image1);
 
	// Checking the arguments
	if (argc!=3)
	{
		printf("Usage: %s Image1.bmp, Image2.bmp\n", argv[0]);
		//return 1;
	}
 
	// Loading the image for quality check
	BITMAPINFOHEADER* pBitmap1 = NULL;
	BITMAPINFOHEADER* pBitmap2 = NULL;
	pBitmap1 = ReadImage(argv[0]);
	if (pBitmap1==NULL)
	{
		printf("Error while reading Image1\n");
	}
 
	scanf("%s",&image2);
        pBitmap2 = ReadImage(argv[1]);
	if (pBitmap2==NULL)
	{
		printf("Error while reading Image2\n");
	}
        // You need to free memory used by bitmaps
        free(pBitmap1);
        free(pBitmap2);
}


Please see comments in code. The end of ReadImage and main procs
 
Share this answer
 
Comments
Member 7909353 22-Jun-12 0:59am    
In main method I am using free(pBitmap1) and free(pBitmap2) in further statements
Albert Holguin 22-Jun-12 11:28am    
If you didn't allocate it, you shouldn't free it. That's what he means.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900