memory leaks

My project is say that it has memory leaks. When I view it in the debugger, it shows the correct values being passed to the pointers.

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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

bool ConfigGetEntry( char *pcFile, char *pcNameMust, char* &pcResult )
{
	char pcBuffer[ 512 ];
	char *pcName, *pcPar, *pcPoint;
	unsigned len;
		
	pcResult=0;

	// Open file
	FILE *fh;
	fopen_s( &fh, pcFile, "r" );
	if ( fh==NULL ) return false;
	
	do
	{
		fgets( pcBuffer, 510, fh );
		len = (unsigned)strlen( pcBuffer );
		if ( pcBuffer[ len-1 ] == '\n' ) --len;
		pcBuffer[ len ] = 0;
		
		pcName = pcBuffer;
		while ( *pcName==' ' || *pcName=='\t' ) ++pcName;
		if ( *pcName==0 || *pcName=='#' ) continue;
		pcPoint = pcName;
		while ( *pcPoint!=' ' && *pcPoint!='\t' && *pcPoint!='#' && *pcPoint!='=' && *pcPoint!=0 ) ++pcPoint;
		if ( *pcPoint==0 || *pcPoint=='#' ) continue;
		pcPar = pcPoint;
		while ( *pcPar==' ' || *pcPar=='\t' || *pcPar=='=' ) ++pcPar;
		*pcPoint=0;
		
		if ( !_strcmpi( pcName, pcNameMust ) )
		{
			pcResult = _strdup( pcPar );
			pcPoint  = pcResult + strlen( pcPar );
			if ( *pcPoint==0 ) return true;
			--pcPoint;
			while( *pcPoint==' ' || *pcPoint=='\t' ) *pcPoint--=0;
			return true;
		}
		
	} while ( !feof( fh ) );
	
	// Close file
	fclose( fh );
	
	return true;
}

char *ConfigGetString( char *pcFile, char *pcName, char *pcDefault )
{
	char *pcRet, *pcRetReal, *pcRetOld;
	if ( !ConfigGetEntry( pcFile, pcName, pcRet ) ) return _strdup( pcDefault );
	if (pcRet==0) return _strdup( pcDefault );
	pcRetOld = pcRet;
	if ( *pcRet=='"' )
	{
		++pcRet;
		pcRet[ strlen(pcRet) - 1 ] = 0;
	}
	
	pcRetReal = _strdup( pcRet );
	free( pcRetOld );
	
	return pcRetReal;
}

unsigned ConfigGetInt( char *pcFile, char *pcName, unsigned uDefault )
{
	char *pcRet;
	unsigned uRet;
	if ( !ConfigGetEntry( pcFile, pcName, pcRet ) ) return uDefault;
	if (pcRet==0) return uDefault;

	uRet = atoi( pcRet );
	free( pcRet );
	
	return uRet;
}


1
2
3
4
5
int main( int argc, char* argv[] )
{
	char* Newspaper =	ConfigGetString("config.ini", "NewsPaperDay", "Monday");
return 0;
}

Its saying that i have memory corrupting at..

 
if ( !ConfigGetEntry( pcFile, pcName, pcRet ) ) return _strdup( pcDefault );


Any idea why?

Also is it normal to have a bad pointer then point data into it to then clear that of the bad pointer. To have valid data now in that pointer.

Last edited on
Because if that condition evaluates to true (!false) then pcRet is destroyed, however, the memory the pcRet points to is leaking.

_strdup allocates memory.
Last edited on
Topic archived. No new replies allowed.