• Advertisement
Sign in to follow this  

An unhandled exception of type 'System.AccessViolationException', but it seems ok....

This topic is 3956 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Alrighty, I've been making a game engine heavily based off of Enginuity, but I decided to give the scripting system a major overhaul.. The current one is/was awful (sorry Mr. Fine). Here is how I've set everything up, in the script files. #begin variables here. #end Ignored here. The reasoning for this was that a game may have several types of units, each with differing health etc. Each batch (begin to end) is independent of the others. In code, it is represented as such: CSettingsManager: std::map< std::string (fileName), ScriptFile > fileMap; ScriptFile: std::list< BATCH > batches; BATCH: std::map< std::string (variable name), std::string* (variable value) > So, each file has several batches which contain their own variables. CSettingsManager manages the files ScriptFile manages it's own batches BATCH contains its own variables Now, when I run ScriptFile straight from the client code-no csettingsmanager - it works just fine, but hard coding values is what the scripting system was for. So then I try to use it through Csettingsmanager, which reads the strings from the file and places them in the correct batch in the correct ScriptFile, I get that error. if comes from these specific lines: in ScriptFile.h
//Returns a variable of a certain name, from the current batch, casted to a certain type. 
	 template< class Type > Type getValue( std::string name )
	 {
		 std::string x = *(*currentBatch_)[name]; 
		std::stringstream str;
		str.unsetf( std::ios::skipws );
		str << (std::string)(*(*currentBatch_)[name]);
		Type result;
		str >> result; 
		return result; 
	 }
Now, when I am debugging it shows me this: www.freewebs.com/mlanet/mystery.JPG So, it looks like batchs (BATCH*) is a 'valid' object.. but I get a System.AccessViolationException when I try to use the 'data' that I get out of it. Since this only occurs when I use Script File via CSettingsManager, I would guess that CSettingsManager does something to the data that it's not supposed to do. I am also guessing that the data has gone out of scope, but with out actually being deleted, hence the apparent 'validity' of *currentBatch_. So, here is the CSettingsManager code:
#ifndef CSETTINGSMANAGER_H
#define CSETTINGSMANAGER_H

//STL Includes.
#include <string>
#include <map> 

//Library Includes.
 
 
#include "Dator.h" 
#include "Singleton.h"
#include "CMMPointer.h" 
#include "ScriptFile.h" 

class CSettingsManager : public Singleton   < CSettingsManager > 
{
public: 
	CSettingsManager();
	virtual ~CSettingsManager(); 

	void setValue( std::string &name, std::string &value, std::string &fileName, int command = 0 );
	ScriptFile& getFile( std::string &name ); 

	void parseSetting( std::string str, std::string fileName ); 
	void parseFile( std::string fileName ); 

protected:
	std::map< std::string, std::string > definesMap_; 
	std::map< std::string, ScriptFile > fileMap_; 

};

#endif //EOF of CSettingsManager.h 




And the CSettingsManager.cpp

//STL Includes 
#include <fstream> 
#include <list> 
#include <stdio.h>

//Engine Includes
#include "CSettingsManager.h"
#include "Dator.h"

#include "CKernel.h" 

CSettingsManager::CSettingsManager()
{
	fileMap_.clear();
	definesMap_.clear();
}

CSettingsManager::~CSettingsManager()
{
}


void CSettingsManager::parseFile( std::string fileName )
{
	std::ifstream in( fileName.c_str() ); 

	if( !in.is_open() )
		return; 
	fileMap_[fileName] = ScriptFile(fileName); 
	
	while( !in.eof() )
	{
		char szBuf[ 1024 ]; 
		in.getline( szBuf, 1024 ); 
		parseSetting( szBuf, fileName ); 
	}
}

ScriptFile& CSettingsManager::getFile( std::string &name )
{
	return fileMap_[ name ];
}


void CSettingsManager::setValue( std::string &name, std::string &value, std::string &fileName, int command )
{	
	if( command == 1 )
	{ 
		if( strcmp( name.c_str(), "BEGIN" ) == 0 )
		{
			fileMap_[fileName].addNewBatch(); 
		}

		if( strcmp( name.c_str(), "DEFINE" ) == 0 )
			definesMap_[name] = value; 
	}
	else
	{
		std::string* data = new std::string( value ); 
		std::string* id = new std::string( name ); 
		fileMap_[fileName].insertNewValue(*id, data ); 
	}
}

void CSettingsManager::parseSetting( std::string str, std::string fileName )
{
	int command = 0; 
	std::string name, value; 

	if( str[ 0 ] == '#' )
	{
		command = 1; 
		str = str.substr( 1 ); //remove the # character from the string. 
	}

	//Find the '='

	int eqPos = str.find( '=' );

	if( eqPos != -1 )
	{
		name = str.substr( 0, eqPos ); 
		value = str.substr( eqPos+1 );
	}
	else if( command == 1 )
	{//No equal sign, so that means this value is a command line. 
		str.find( ' ' ) == -1 ? name = str : name = str.substr( 0, str.find(' ') ); 
		str.find( ' ' ) == -1 ? value = str : value = str.substr( str.find(' ')+1 );  
	}
	setValue( name, value, fileName, command ); 
}




Share this post


Link to post
Share on other sites
Advertisement
ROFL!!!
I can't believe how stupid I am.

If you look at the picture (as I was awhile ago), read closely-
I pass in 'height'
trying to get 'screenHeight'
This is going on my list of top ten stupid mistakes.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement