GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 15-Nov-2005, 03:52
dlare9 dlare9 is offline
New Member
 
Join Date: Oct 2005
Posts: 12
dlare9 is on a distinguished road
Unhappy

Memory cannot be read?


Hi there it's me again.
this is with regards to the thread Debug Assertion Failed!
well i've finally figured out what causing the problem,i failed to add a enviroment variable w/c value is the location of the cfg file.

i've pre-run the program and i it works fine.so i decided to continue to code.
now the problem is that when i run the program again i've got this message
(http://i20.photobucket.com/albums/b236/geebee0901/popup.jpg)

can any one help figure out what's wrong. i'm pretty much stuck here

thanks in advance,

dlare9






here are my codes...

RMServer.c

CPP / C++ / C Code:
// RMServer side
#include "RMServer.h"

char msg[50]; 
char logfile[100]="";
int error;
struct RMConfig cfg;
extern int WriteToLog(char* str);
extern void displayVersion();

//all printf() are used for debugging
int main(){ 

	WSADATA wsda; 
	WSAStartup(0x0101,&wsda); 

	// Start Logger
	printf("0   0");
	logStart();
	// end Logger


	if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
	{ 
	//printf("Socket error..."); 
	WriteToLog("Socket error...");
	
	return 0; 
	} 

	server.sin_addr.s_addr=INADDR_ANY;	/*INADDR_ANY will simply put your IP */ 
	server.sin_port=htons(PORT);		/*htons means host to network short */
	server.sin_family=AF_INET; 


	if(bind(sockfd,(struct sockaddr*)&server,sizeof(struct sockaddr))==-1)
	{ 
	//printf("Cannot bind..."); 
	WriteToLog("Error! Cannot bind...");
	return 0; 
	} 

	if(listen(sockfd,BACKLOG))/*Listening */
	{ 
	//printf("Error Listening...");
	WriteToLog("Error! Listening...");
	return 0; 
	} 

	while (1)
	{ 

		int size=sizeof(struct sockaddr_in); 

		if((sockfd2=accept(sockfd,(struct sockaddr*)&client,&size))==-1)
		{ 
		//printf("Accept Error..."); 
		WriteToLog("Accept Error!...");
		return 0; 
		} 

		if((n_bytes=recv(sockfd2,msg ,50,0))==-1){ 
		//printf("Error Recv..."); 
		WriteToLog("Error Recv!...");
		return 0; 
		} 

		msg[n_bytes]='\0'; 

		printf("Client's Message:%s",msg); 
		WriteToLog("Client's Message : ");
		WriteToLog(msg);

	} 

	WSACleanup(); 
	closesocket(sockfd); /*used to close the socket */ 
	closesocket(sockfd2); 

	return 0; 
}// end main 



// log init
int logStart() 
{ 
    int error=0;
	char logfile[100]="";
	char *rmConfig;

	printf(" logStart()  ");
	if((rmConfig = getenv(RM_SYS_VAR)) != NULL)
	{
		sprintf(CONFIG_FILE,"%s",rmConfig);	
	}
	else 
	{
		printf("no cfg file found!");
		return 1;
	}

	//strcpy(CONFIG_FILE ,"C://RISK//RMResource//RM.cfg");  //hard coded the location of config file
	printf("0   ");
	cfg = initConfig();
	printf("1   ");
	cfg = getLogger(LOG_CONFIG,cfg);
	printf("2   ");
	strcpy(logfile,cfg.log.logDirectory);
	printf("3  ");
	strcat(logfile, cfg.log.logFile);
	printf("4  ");
	strcpy(LOGFILE,logfile);
	//printf("logfile  %s  ",logfile);
	error = WriteToLog("Monitoring started.");
	printf("gerald  ");
	if (!error)
	{
		printf("deeh  ");
		displayVersion();
		sprintf(logfile,"RM CONFIG FILE = %s",CONFIG_FILE);
		WriteToLog(logfile);
	}
    return(error); 
}



RMUtil.c

CPP / C++ / C Code:
#include "RMUtil.h"

char errorNum[20]="";
char tempVal[100]="";

int WriteToLog(char* str)
{

	time_t t;
	FILE* log;
    char today[50]="";
	t = time(NULL);
	
	printf("b  ");
	sprintf(today,"%s",ctime(&t));
	printf("c  ");
	today[strlen(today)-1] = '\0';
	printf("d  ");

	if(LOGFILE == '\0')
	{
	printf("e  ");
	log = fopen(LOGFILE, "a+");
	printf("f  ");
	}
	
	if (log == NULL)
	{
	printf("g  ");
	return -1;
	}
	
	printf("h  ");
	fprintf(log, "%s %s\n", today, str);  // i think this is causing the problem but don't know wat to do
	printf("i  ");
	fclose(log);
	printf("j  ");
	return 0;
}

int writeToFile(char *file, char* str)
{
	char logger[100]="";
	time_t t;
	FILE* log;
    char today[50]="";
	t = time(NULL);

	sprintf(today,"%s",ctime(&t));
	today[strlen(today)-1] = '\0';

	log = fopen(file, "w");
	if (log == NULL){
		sprintf(logger, "FAILED to open file: %s. Make sure that the directory path exists.", file);
		WriteToLog(logger);
		return 1;
	}
	fprintf(log, "%s %s\n", today, str);
	fclose(log);
	return 0;
}



char* removeNewline(char line[])
{
	if (line[strlen(line)-1] == '\n')
	{
		line[strlen(line)-1] = '\0';
	}
	return line;
}


char* getValue(char *line)
{
	char value[130]="";
	char str[130]="";
	int i=0, k=0;

	strcpy(value, line);
	if(value[0] == ' ')
	{
		while(value[i] == ' '){
			i++;
		}
		while(value[i] != '\0' && value[i] != '\n'){
			str[k]= value[i];
			k++;i++;
		}
		str[k]='\0';
		strcpy(value,str);
	}

	if(value[0] == '\"'){
		i=1;k=0;
		while(value[i] != '\0' && value[i] != '\"'  && value[i] != '\n'){
		       str[k] = value[i];
		       i++; k++;
		}
		str[k]='\0';
		strcpy(value,str);
	}

	return removeNewline(value);
	
}


struct RMConfig initConfig()
{
	
	struct RMConfig cfg;
	struct tm time;
	
	time.tm_hour =0;
	time.tm_mday =1;
	time.tm_min =0;
	time.tm_mon = 1;
	time.tm_year = 1900; 

	strcpy(cfg.log.logFile,"");
	strcpy(cfg.log.logDirectory,"");
	strcpy(cfg.log.logArchiveDirectory,"");
	strcpy(cfg.log.nextLogClearStr,"");

	strcpy(cfg.version.build_date_Str,"");
	strcpy(cfg.version.version,"");
	strcpy(cfg.version.system_name,"");
	
	return cfg;
	
}


struct RMConfig getLogger(int configType, struct RMConfig cfg)
{

	FILE *configFile;

	char msg[30];
	char val[200];
	char ch;
	char cfgTitle[50]="";
	
	if ((configFile = fopen( CONFIG_FILE, "r" )) == NULL)
	{
		return cfg;
	}
	
	if (configType == LOG_CONFIG){
		strcpy(cfgTitle,LOG_CONFIG_TITLE);
	}
	while(!feof(configFile)){
		fscanf(configFile,"%s", msg);
		if (strcmp(cfgTitle,msg) == 0){
			
			if (configType == LOG_CONFIG)	{
				while(!feof(configFile) && (ch = fgetc(configFile)) != EOF){
					if (ch == '#')
						return cfg;
					if (ch != '\n'){
						fseek(configFile,-1, SEEK_CUR);
						fscanf(configFile,"%s", msg);
						fgets(val,sizeof(val),configFile);
						strcpy(val,getValue(val));

						if(!strcmp(msg,LOGFILE_LABEL))
						{
							strcpy(cfg.log.logFile,val);
						}
						if(!strcmp(msg,LOG_DIRECTORY_LABEL))
						{
							strcpy(cfg.log.logDirectory,val);
						}
						if(!strcmp(msg,LOG_ARCHIVE_DIRECTORY_LABEL))
						{
							strcpy(cfg.log.logArchiveDirectory,val);
						}
				
					}
				}
			}
		}
	}
	fclose(configFile);
	return cfg;
}


char* getPropertyValue(char *filename, char *property)
{

	char log[100]="";
	char val[100]="";
	char key[100]="";
	//char ch;
	FILE *propertyFile;
	
	if ((propertyFile = fopen(filename, "r" )) == NULL)
	{
		sprintf(log, "FAILED to open file: %s. Make sure that the file and the directory path exists.", filename);
		WriteToLog(log);
	}
	else
	{
		while(!feof(propertyFile)){
			fscanf(propertyFile,"%s", key);

			if (strcmp(key,property) == 0){
				fgets(val,sizeof(val),propertyFile);
				strcpy(val,getValue(val));
			}
		}	
		fclose(propertyFile);
	}
	if(strcmp(val,"")==0)
	{
		sprintf(log,"WARNING: Property name '%s' is not found in %s", property,filename);
		WriteToLog(log);
	}
	strcpy(tempVal,"");
	strcpy(tempVal,val);
	
	return tempVal;
}



struct RMConfig getConfig(int configType,struct RMConfig cfg)
{


	char log[100]="";
	char cfgTitle[50]="";
//	sprintf(log,"Getting configuration type: %d",configType );
//	WriteToLog(log);
	if (configType == LOG_CONFIG){
		strcpy(cfg.log.logFile,getPropertyValue(CONFIG_FILE, LOGFILE_LABEL));
		strcpy(cfg.log.logDirectory,getPropertyValue(CONFIG_FILE, LOG_DIRECTORY_LABEL));
		strcpy(cfg.log.logArchiveDirectory,getPropertyValue(CONFIG_FILE, LOG_ARCHIVE_DIRECTORY_LABEL));
	}
	if (configType == VERSION_CONFIG){
		strcpy(cfg.version.system_name,getPropertyValue(CONFIG_FILE, SYSTEM_NAME_LABEL));
	}
	return cfg;
}



void displayVersion()
{
	struct RMConfig cfg;
	char log[100]="";
	cfg = initConfig();
	cfg = getConfig(VERSION_CONFIG,cfg);
	sprintf(log,"%s",cfg.version.system_name);
	WriteToLog(log);

	sprintf(log,"Version: %s",VERSION);
	WriteToLog(log);
	sprintf(log,"Build date: %s",BUILD_DATE);
	WriteToLog(log);
}



RMServer.h
CPP / C++ / C Code:
#include <stdio.h>
#include <winsock.h>
#include "RMUtil.h"
#define PORT 1200 
#define BACKLOG 4 
struct sockaddr_in server; 
struct sockaddr_in client;
int sockfd,sockfd2,n_bytes; 
int main();
int logStart();


RMUtil.h
CPP / C++ / C Code:
#include <string.h>
#include <stdio.h>
#include <time.h>
#include <ctype.h>
#include <errno.h>
#include <stdlib.h>
#include "RMConstants.h"

char LOGFILE[200];
char CONFIG_FILE[100];

struct logInfo
{
  struct tm nextLogClear;

  char logFile[ 30 ];
  char logDirectory[ 200 ];
  char logArchiveDirectory[ 200 ];
  char nextLogClearStr[ 16 ];
  int daysBeforeArchiveLog;

};

struct versionInfo{
	struct tm build_date;

	char build_date_Str[20];
	char version[8];
	char system_name[50];
};


struct RMConfig{
	struct logInfo log;
	struct versionInfo version;
};

int WriteToLog(char* str);
int writeToFile(char *file, char* str);
char* removeNewline(char line[]);
char* getValue(char *line);
struct RMConfig initConfig();
struct RMConfig getLogger(int configType, struct RMConfig cfg);
char* getPropertyValue(char *filename, char *property);
struct RMConfig getConfig(int configType,struct RMConfig cfg);
void displayVersion();


RMConstants.h
CPP / C++ / C Code:
#include<time.h>
#define BUILD_DATE "2005-11-11"
#define VERSION "beta 0.0.1"
#define LOG_CONFIG_TITLE "#log" 
#define LOGFILE_LABEL "log_file" 
#define LOG_DIRECTORY_LABEL "log_directory"
#define LOG_ARCHIVE_DIRECTORY_LABEL "log_archive_directory"
#define RM_SYS_VAR "PNL"
#define VERSION_CONFIG_TITLE "#version"
#define SYSTEM_NAME_LABEL "system_name"
#define LOG_CONFIG 259
#define VERSION_CONFIG 260


and btw.. you have to create an enviroment variable called "RM" having the value of the cfg file (i.e. "C:\RISK\RMResource\RM.cfg"). And also a you have to create a RM.log file located where the RM.cfg is placed.

and the RM.cfg file contains the ff:

#version
system_name RISK MANAGEMENT

#log
log_file RM.log
log_directory C:\\RISK\RMResource\
log_archive_directory C:\\RISK\RMResource\log archive\
Attached Images
File Type: jpg popup.jpg (15.2 KB, 23 views)
  #2  
Old 15-Nov-2005, 09:28
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,793
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Memory cannot be read?


Quote:
Originally Posted by dlare9
i'm pretty much stuck here


Make the program tell you!

You have little printf statements that print "a" "b", etc at various points so that you can check your progress. Make them more descriptive. Here is what I might do in the function that you think is bombing:

CPP / C++ / C Code:
int WriteToLog(char* str)
{

	time_t t;
	FILE* log;
    char today[50]="";
	t = time(NULL);
	
	printf("\n\nb in WriteToLog str:<%s>\n", str);
	sprintf(today,"%s",ctime(&t));
	printf("c: today <%s>\n", today);
	today[strlen(today)-1] = '\0';
	printf("d, today <%s>\n", today);

        printf("d1: LOGFILE: %p <%s>, log %p\n", LOGFILE, LOGFILE, log);
	if(LOGFILE == '\0') /* <=== What's up with this???  */
	{
	printf("e opening log\n");
	log = fopen(LOGFILE, "a+");
	printf("f log %p\n", log);
	}
	
        printf("log: %p\n", log);
	if (log == NULL)
	{
	printf("g log == NULL\n");
	return -1;
	}
	
	printf("h today <%s>\n", today);
        printf("h1 str  <%s>\n", str);
	printf("h2: <%s> <%s>\n", today, str);
        printf("h3 Calling printf(log)\n");
	fprintf(log, "%s %s\n", today, str);  // i think this is causing the problem but don't know wat to do
        printf("h4 back from printf(log)\n");
	printf("i calling fclose(log)\n");
	fclose(log);
	printf("j back from fclose(log) ");
	return 0;
}

Note the comment that I added: I think you aren't calling fopen(). Calling fclose() with a pointer whose value is not something that you got from fopen() is a big no-no. fclose() tries to flush the stream buffers that were allocated with fopen(). If the argument to fclose() is invalid, fclose() could generate the error message you saw, since it's trying to read stuff from who-knows-where.

Now, my analysis could be wrong. That's why I didn't just put some debug info around the place that I think generates the problem. I put them everwhere I could think of in the staements leading up to the point of failure. Sometimes a failure doesn't occur at the point of the actual problem, but is the result of things that happened (or didn't happen) some time before. So: I start at the beginning of the function and show everything that's happening. In terms of time spent debugging, I don't think that is overkill. But that's just an opinion. Your Mileage May Vary.

Anyhow, the idea is to use the program itself to help you debug the program.

Regards,

Dave
  #3  
Old 16-Nov-2005, 00:42
dlare9 dlare9 is offline
New Member
 
Join Date: Oct 2005
Posts: 12
dlare9 is on a distinguished road
Smile

Re: Memory cannot be read?


Quote:
Originally Posted by davekw7x
Make the program tell you!
....So: I start at the beginning of the function and show everything that's happening.

Anyhow, the idea is to use the program itself to help you debug the program.

point well taken, i guess i have a bad way of debuging a program
btw thanks for pointing out the fclose(), i guess i never consider it as a problem until now.

the program now works thanks to your advice.


regards,
dlare9

off to take the easy route
  #4  
Old 16-Nov-2005, 08:03
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,793
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Memory cannot be read?


Quote:
Originally Posted by dlare9
point well taken, i guess i have a bad way of debuging a program :

Actually, you had a really good start: print stuff from time to time to see how far it got before it crashed.
I only suggested a slight enhancement: learn what the heck it was working on when it took the dive.

Regards,

Dave
 
 

Recent GIDBlogToyota - 2008 November Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Airport Log program using 3D linked List : problem reading from file batrsau C Programming Language 11 29-Feb-2008 08:44
Pointer Usage in C++: Beginner to Advanced varunhome C++ Forum 0 19-Aug-2005 10:25
[Tutorial] Pointers in C (Part II) Stack Overflow C Programming Language 0 27-Apr-2005 18:36
[Tutorial] Pointers in C (Part I) Stack Overflow C Programming Language 1 08-Apr-2005 19:35

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 12:11.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.