[Expat-discuss] Access Violation

Alan Bort alanbort at gmail.com
Fri Dec 29 13:15:04 CET 2006


Hi, I am programming on windows, and this piece of code is giving me
an access violation when inicio_h ends. (at least that's how far I was
able to track the problem)
The Access Violation occurs in the call to XML_Parse();

/*###STRUCTURES###*/


typedef struct _PLAYERDATA {
	BOOL bEnabled;
	BOOL bPlayerMe; /* indica que este jugador, ya sea una pc o un
humano, pertenece a esta PC */
	CHAR cName[13];
	
	/* 0: controlado por el usuario, 1: CPU facil - 4: CPU tramposo, 5:
esperar jugadas de la red */
	UINT uiMode;
	/* puntero a una estructura de ficha, esto nos da acceso al linked
list de las fichas */
	/* NULL si no tenemos información de las fichas de este jugador, nos
basamos luego en uiFichasCount para dibujar la cantidad de fichas del
player */
	FICHA * fFicha;
	UINT uiFichasCount;
	UINT uiScore;
	int iGuiOrientation; /* indica en que angulo se dibuja a este player
en la pantalla, esto se define al crear la jugada */
	
	/* declarar más cosas */
} PLAYERDATA;

typedef struct _GAMEDATA {
	BOOL bNetworkGame;
	BOOL bHostMode;
	CHAR cPath[MAX_PATH];
	PLAYERDATA pdPlayers[4];
	UINT uiMaxPoints;
	UINT uiTimePerTurn;
	UINT uMe; /* jugador humano */
	FICHACOLOCADA * fcFichaCentro;
	/* uiGameStage indica que tiene que ocurrir en este momento en el juego:
		0: valor inicial, esperando configuración de partida
		1: inicializar juego, se crean o se leen los XMLs de inicio de juego
		   (depende si es local o network, si es host o client)
		2: leer el dato uiCurrentTurn para ver quien debe jugar y hacer algo
de acuerdo a que jugador es
		3: Juego terminó, pero el puntaje maximo no fue alcanzado, volver a 1
		4: juego terminó, y el puntaje máximo fue alcanzado.
	*/
	UINT uiGameStage;
	UINT uiCurrentTurn; /* jugada 1, 2, 3, 4, ... */
	UINT uQuedanFichas; /* cantidad de fichas que quedan para repartir a
los jugadores */
} GAMEDATA;

typedef struct {
	UINT uColor1;	//especifica color 1, va seguido al extremo de ultima ficha
	UINT uColor2; //especifica color 2
	UINT uExtremo; //indica el extremo
	BOOL bDomino;	//si ya un jugador canto domino y termino la partida
	BOOL bRecibio; //si un jugador recibio ficha
	BOOL bPaso; //si jugador no juega en su turno	
	UINT uNumJugada; //el numero de jugada
} JUGADA;


/*XML.C CODE*/

void obtener_fichas(char *name)
{
	char buffer[BUFSIZ];
	char file[MAX_PATH];
	char error[BUFSIZ];
	FILE *arch;
	XML_Parser parser;
	int done;
	//static CONFIG config;
//	struct stat estado;
	//Rellena el formato fichas-nombrejugador.xml
	sprintf(file,"%s\\fichas-%s.xml",gpgdJuego->cPath,name);
	MessageBox(NULL,file,"Error",MB_OK|MB_ICONEXCLAMATION);
	if((arch = fopen(file,"rb"))==NULL){
		MessageBox(NULL,"Error al abrir el archivo de
fichas","Error",MB_OK|MB_ICONEXCLAMATION);
		return;
	}
	//Resetea todos los componentes de config
	//reset_config(&config);
	reset_gamedata(gpgdJuego);

	/*Aqui comienza el parseo*/
	//Crea un parser con codificacion UTF-8
	parser = XML_ParserCreate("UTF-8");
	
	//paso estructura CONFIG al parser, para que las funciones
	//que procesan tags puedan operar con ella
	//la reciben en el campo *UserData
	XML_SetUserData(parser,gpgdJuego);
	//Funciones que van a procesar el principio y fin de tags>
 	//para el caso de varios atributos en multilíneas
 	XML_SetElementHandler(parser,inicio_h,fin_h);
 	//para el caso de atributos simples en una sola linea
 	XML_SetCharacterDataHandler(parser,char_h);
	
	do {
	    size_t len = fread(buffer, 1, sizeof(buffer), arch);
	    done = len < sizeof(buffer);
	    //Verifica si hay error en el xml
		int o;
		int p;
		o = XML_Parse(parser, buffer, len, done);
		MessageBox(NULL,"Parsed","Parsing",MB_OK);
		p = (o == XML_STATUS_ERROR);
		
	   if (p) {
	      sprintf(error,"%s at line
%d\n",XML_ErrorString(XML_GetErrorCode(parser)),XML_GetCurrentLineNumber(parser));
			MessageBox(NULL,error,"Error:",MB_OK);
	      //modificar esta parte para api
	      return;
	    }
  	}while (!done);
  	
  	fclose(arch);
	//remove(file);
	XML_ParserFree(parser);
	
	return /*&config*/;
}

static void inicio_h(void *userData,const XML_Char *name,const XML_Char **atts)
{
	int i = 0;
	static UINT uColor1;
	static UINT uColor2;
	static BOOL bEmpieza;
	static BOOL bDone;
//	CONFIG *conf = (CONFIG *)userData;
	GAMEDATA *juego = (GAMEDATA *)userData;
	
	int a;
	int b;
	a = lstrcmpi(name,"jugador");
	b = lstrcmpi(name,"ficha");
	//MessageBox(NULL,name,"CHAU",MB_OK);
	char c[100];
	sprintf(c,"a:%d, b:%d | el or da:%d",a,b,((!a) || (!b)));
	MessageBox(NULL,c,"Test",MB_OK);
	//if((!strcmp(name,"jugador"))||(!strcmp(name,"ficha"))){
	if ((!a) || (!b)){
		//verifica si atributos == NULL, si no entonces
		//contiene informacion importante
		MessageBox(NULL,"Hola","CHAU",MB_OK);
		if (atts == NULL)
			return;
		while(atts[i]){
			if(!strcmp(atts[i],"numero")){
				i++;
				switch(convertir(atts[i++])){
					case 1:	strcpy(juego->pdPlayers[0].cName,atts[++i]); break;
					case 2:	strcpy(juego->pdPlayers[1].cName,atts[++i]); break;
					case 3:	strcpy(juego->pdPlayers[2].cName,atts[++i]); break; 	
					case 4:	strcpy(juego->pdPlayers[3].cName,atts[++i]); break;	
				}
				i++;
			}else{
				bEmpieza = FALSE;
				bDone = FALSE;
				 if(!strcmp(atts[i],"color1")){
					uColor1 = convertir(atts[++i]);
					i++;
				 }else if(!strcmp(atts[i],"color2")){
					uColor2 = convertir(atts[++i]);
					i++;
				 }else if(!strcmp(atts[i],"empieza")){
						i++;
						if(!strcmp(atts[i],"si")){
							bEmpieza = TRUE;
							bDone = TRUE;
						   i++;
						}
				 }
				if (bDone)
					AgregarColor(juego->pdPlayers[juego->uMe].fFicha,uColor1,uColor2,bEmpieza);
			}
		}
	}
	else
		if(!strcmp(name,"maxtiempo"))
			gpgdJuego->uiTimePerTurn=1;
		else
			if(!strcmp(name,"maxpuntos"))
				gpgdJuego->uiMaxPoints = 1;
	MessageBox(NULL,"Parsed","Parsing",MB_OK);
	return;
}

static void fin_h(void *userData,const XML_Char *name)
{
	int a=0;
	a ++;
	/*if (!strcmp(name,"maxtiempo")) {
		gpgdJuego->uiTimePerTurn=0;
	} else if (!strcmp(name,"maxpuntos")) {
		gpgdJuego->uiMaxPoints=0;
	} */
	return;
}

static void char_h(void *userData,const XML_Char *s,int len)
{
	int a=0;
	a ++;
	/*char buffer[64];
//	CONFIG *conf = (CONFIG *)userData;
	GAMEDATA *gamedata = (GAMEDATA *)userData;
		
	strncpy(buffer,s,len);
	buffer[len] = '\0';
	
	if(gpgdJuego->uiTimePerTurn){
		gamedata->uiTimePerTurn = atoi(buffer);
	}else if(gpgdJuego->uiMaxPoints){
		gamedata->uiMaxPoints = atoi(buffer);
	}*/
	return;
}

it is supposed to parse this file, and the call to obtener_fichas is
the following:

                        char tmp_cName[13];
			lstrcpy(tmp_cName,gpgdJuego->pdPlayers[0].cName);
			obtener_fichas(tmp_cName);
			for (int i = 0; i<4;i++){
				if (gpgdJuego->pdPlayers[i].bEnabled){
					if (!lstrcmpi(tmp_cName,gpgdJuego->pdPlayers[i].cName)){
						gpgdJuego->pdPlayers[i].bPlayerMe = TRUE;
						gpgdJuego->uMe = i;
						break;
					}
				}
			}


/**FILE**/
<?xml version="1.0" encoding="UTF-8"?>
<fichas-obiwan>
	<jugadores>
		<jugador numero="1" nombre="obiwan"/>
		<jugador numero="2" nombre="CHAU"/>
		<jugador numero="3" nombre="CHAU"/>
		<jugador numero="4" nombre="CHAU"/>
	</jugadores>
	<maxtiempo>60</maxtiempo>
	<maxpuntos>100</maxpuntos>
	<fichas>
		<ficha color1="A" color2="N"/>
		<ficha color1="R" color2="V"/>
		<ficha color1="Z" color2="Z"/>
		<ficha color1="Z" color2="G"/>
		<ficha color1="Z" color2="B"/>
		<ficha color1="Z" color2="N"/>
		<ficha color1="N" color2="V"/>
		<ficha color1="R" color2="R"/>
		<ficha color1="A" color2="Z"/>
	</fichas>
</fichas-obiwan>


The Access Violation occurs in the call to XML_Parse();


More information about the Expat-discuss mailing list