Why in the world?

BlitzPlus Forums/BlitzPlus Programming/Why in the world?

Pineapple(Posted 2009) [#1]
What kinds of things would cause ReadString() to cause a random crash - no debug errors, no nothing? And it only crashes on the ReadString() lines after the line "Local dNAME$=ReadString(fil)".


The code that crashes:
Function ImportChunk(path$)
	fil=ReadFile(path)
	If ReadInt(fil)<>1263421507 Then 
		Notify path+" is not a valid exported CHUNK file.",1
		AddLogMsg "attempted to import chunk from an invalid file"
		Return 0
	EndIf
	d.MODL=New MODL
	d\MTYP=ReadByte(fil)
	d\MNDX=ReadByte(fil)
	Local dNAME$=ReadString(fil)
	For o.MODL=Each MODL
		If dNAME=d\name Then
			Notify "A chunk already exists in the currently opened mesh with the same name as the one you are trying to import."+Chr(13)+"If you still wish to import his chunk, please rename the existing chunk, "+o\name+" to something else":AddLogMsg "duplicate chunk name in import"
			Delete d
			Return 0
		EndIf
	Next
	d\NAME=dNAME
	d\hasPRNT=ReadByte(fil)
	d\PRNT=ReadString(fil)
	d\hasFLGS=ReadByte(fil)
	d\FLGS=ReadInt(fil)
	d\TRAN=ReadString(fil)
	d\hasGEOM=ReadByte(fil)
	d\BBOX=ReadString(fil)
	d\SEGMnum=ReadByte(fil)
	If d\SEGMnum>0 Then
		For xx=1 To d\SEGMnum
			d\SEGM[xx]=ReadString(fil)
			d\MATI[xx]=ReadInt(fil)
		Next
	EndIf
	hasENVL=ReadByte(fil)
	ENVL=ReadString(fil)
	CloseFile fil
End Function



The working export code:
				AddLogMsg "exporting chunk "+activec\name
				fil=WriteFile("Chunks\"+activec\name+".chunk")
				WriteInt fil,1263421507 ;CHNK
				WriteByte fil,activec\MTYP
				WriteShort fil,activec\MNDX
				WriteString fil,activec\NAME
				WriteByte fil,activec\hasPRNT
				WriteString fil,activec\PRNT
				WriteByte fil,activec\hasFLGS
				WriteInt fil,activec\FLGS
				WriteString fil,activec\TRAN
				WriteByte fil,activec\hasGEOM
				WriteString fil,activec\BBOX
				WriteByte fil,activec\SEGMnum
				For xx=1 To activec\SEGMnum
					If activec\SEGMnum=0 Then Exit
					WriteString fil,activec\SEGM[xx]
					WriteInt fil,activec\MATI[xx]
				Next
				WriteByte fil,hasENVL
				WriteString fil,ENVL
				CloseFile fil
				AddLogMsg "chunk exported"
				Notify "Exported chunk "+activec\name+" to "+CurrentDir()+"Chunks\"+activec\name+".chunk"



Matty(Posted 2009) [#2]
First thing I noticed...MNDX - you are writing a short but reading a byte.


Pineapple(Posted 2009) [#3]
Yes, that turned out to be the problem. Thanks