Problem with heap

Discussion in 'Win32' started by johny_1015_1981, Feb 22, 2009.

  1. johny_1015_1981

    johny_1015_1981 New Member

    Joined:
    Oct 5, 2008
    Messages:
    19
    Likes Received:
    0
    Trophy Points:
    0
    Can any body tell me what is wrong with the program
    program perform well but messup after finishing
    GlobalFree(tData);
    Some one please give me a solution
    Code:
    #include <math.h>
    #include <windows.h>
    #include <stdio.h>
    #include <fcntl.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <io.h>
    #include <stdlib.h>
    #include <errno.h>
    #include <share.h>
    #include <io.h>
    #include <sys/locking.h>
    #include <share.h>
    #include <string.h>
    
    int base64(char src[4], int bytes, char ret[4]);
    void Base64(wchar_t SourceFileName[500],wchar_t DestinationFileName);
    
    char text[64]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    
    void main()
    {	
    	Base64(TEXT("E:\\c7.bmp"),TEXT("E:\\OUTPUT.txt"));
    }
    
    void Base64(wchar_t SourceFileName[500],wchar_t DestinationFileName[500])
    {
    	int SourceFile;
    	int DestinationFile;
    	int data;
    	char *tData;
    	int bytesWritten;
    	int bytesRead;
    	DWORD err;
    	char *b64;
    	int towrite;
    	int count;
    	
    	b64=GlobalAlloc(GMEM_FIXED,5);
    	tData=GlobalAlloc(GMEM_FIXED,4);
    	SourceFile = _wsopen(SourceFileName,_O_RDONLY|_O_BINARY,_SH_DENYWR,_S_IREAD);	
    	if(EACCES==SourceFile)
    		printf("Given path is a directory, or file is read-only, but an open-for-writing operation was attempted.\n");
    	else if(EEXIST==SourceFile)
    		printf("_O_CREAT and _O_EXCL flags were specified, but filename already exists.\b");
    	else if(EINVAL==SourceFile)
    		printf("Invalid oflag or shflag argument.\n");
    	else if(EMFILE==SourceFile)
    		printf("No more file descriptors available.\n");
    	else if(ENOENT==SourceFile)
    		printf("File or path not found.\n");
    	DestinationFile=_wsopen(DestinationFileName,_O_CREAT|_O_BINARY|_O_RDWR,_SH_DENYWR,_S_IREAD|S_IWRITE);		
    	if(EACCES==DestinationFile)	
    		printf("Given path is a directory, or file is read-only, but an open-for-writing operation was attempted.\n");
    	else if(EEXIST==DestinationFile)
    		printf("_O_CREAT and _O_EXCL flags were specified, but filename already exists.\b");
    	else if(EINVAL==DestinationFile)
    		printf("Invalid oflag or shflag argument.\n");
    	else if(EMFILE==DestinationFile)
    		printf("No more file descriptors available.\n");
    	else if(ENOENT==DestinationFile)
    		printf("File or path not found.\n");
    	
    	err=GetLastError();
    	if (err==ERROR_ALREADY_EXISTS)
    	{
    		_close(DestinationFile);
    		DeleteFile(DestinationFileName);
    		DestinationFile=_wsopen(DestinationFileName,_O_CREAT|_O_BINARY|_O_RDWR,_SH_DENYWR,_S_IREAD|S_IWRITE);
    	}
    
    	count = 0;
    	while((bytesRead=_read(SourceFile,tData,3))==3)
    	{	
    		tData[4]=0;
    		towrite=base64(tData,bytesRead,b64);
    		if (( bytesWritten = _write(DestinationFile,b64,towrite)) == -1 )
    		{
    			switch(errno)
    			{
    				case EBADF:
    					perror("Bad file descriptor!");
    					return;
    				case ENOSPC:
    					perror("No space left on device!");
    					return;
    				case EINVAL:
    					perror("Invalid parameter: buffer was NULL!");
    					return;
    				default:
    					// An unrelated error occured 
    					perror("Unexpected error!");
    					return;
    			}
    		}
    		tData[3]='\0';
    		count=count+bytesWritten;
    		if (count==76)
    		{
    			b64[0]='\n';
    			b64[1]='\0';
    			bytesWritten = _write(DestinationFile,b64,1);
    			count = 0;
    		}
    	}
    	if(bytesRead<3 && bytesRead>0)
    	{
    		towrite=base64(tData,bytesRead,b64);
    		tData[0]='\0';
    		if (( bytesWritten = _write(DestinationFile,b64,towrite)) == -1 )
    		{
    			switch(errno)
    			{
    				case EBADF:
    					perror("Bad file descriptor!");
    					break;
    				case ENOSPC:
    					perror("No space left on device!");
    					break;
    				case EINVAL:
    					perror("Invalid parameter: buffer was NULL!");
    					break;
    				default:
    					// An unrelated error occured 
    					perror("Unexpected error!");
    			}
    
    		}
    	}
    	//_write(DestinationFile,tData,bytesRead);	
    	_close(SourceFile);
    	_close(DestinationFile);
    	GlobalFree(b64);
    	[COLOR="Red"]GlobalFree(tData);[/COLOR]
    	
    }
    
    int base64(char src[4],int bytes,char ret[4])
    {	
    	unsigned int ConvertedINT;	
    	int i,j;	
    	int tmp;
    	int TmpData;	
    
    	ConvertedINT=0;
    	
    	for(i=bytes-1,j=0;i>=0;i--,j++)
    	{
    		ConvertedINT = ConvertedINT+src[i]*pow(256,j);
    	}
    	if(bytes==1)
    	{
    		ConvertedINT=ConvertedINT*16;
    		TmpData=ConvertedINT;	
    		tmp = TmpData/64;
    		ret[0]=text[tmp];
    		TmpData=TmpData-tmp*64;
    		ret[1]=text[TmpData];
    		ret[2]='=';
    		ret[3]='=';
    		ret[4]='\0';
    
    	}
    	else if(bytes==2)
    	{
    		ConvertedINT=ConvertedINT*4;
    		TmpData=ConvertedINT;
    		tmp = TmpData/4096;
    		ret[0]=text[tmp];
    		TmpData=TmpData-tmp*4096;
    	
    		tmp = TmpData/64;
    		ret[1]=text[tmp];
    		TmpData=TmpData-tmp*64;
    		ret[2]=text[TmpData];
    		ret[3]='=';
    		ret[4]='\0';
    	}
    	else
    	{
    		TmpData=ConvertedINT;
    		tmp=TmpData/262144;
    		ret[0]=text[tmp];
    		TmpData=TmpData-tmp*262144;
    
    		tmp = TmpData/4096;
    		ret[1]=text[tmp];
    		TmpData=TmpData-tmp*4096;
    	
    		tmp = TmpData/64;
    		ret[2]=text[tmp];
    		TmpData=TmpData-tmp*64;
    		ret[3]=text[TmpData];
    		ret[4]='\0';
    	}
    	return strlen(ret);
    }
    
     
  2. johny_1015_1981

    johny_1015_1981 New Member

    Joined:
    Oct 5, 2008
    Messages:
    19
    Likes Received:
    0
    Trophy Points:
    0
    OK,
    I actually asked this question long time ago and nobody ans me.
    But I figured the problem

    The problem was I alocate 4 bytes for tData variable
    tData=GlobalAlloc(GMEM_FIXED,4);
    But I accessed 5th byte
    tData[4]=0;
    That is why the program failed to release heap. The limit is from 0 to 3
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice