about summary refs log tree commit diff
diff options
context:
space:
mode:
authoroy <Tom_Adams@web.de>2010-09-28 12:46:15 +0200
committeroy <Tom_Adams@web.de>2010-09-28 12:46:15 +0200
commitc172c24fd1fbf6a430e1852e62e72f6e0cfeeb63 (patch)
treebdf71ecb487376732cf05adc199f3acff9606b9e
parent25ceafaf2219f2d72d3748de6f789cd6d9dd5d2f (diff)
downloadzcatch-c172c24fd1fbf6a430e1852e62e72f6e0cfeeb63.tar.gz
zcatch-c172c24fd1fbf6a430e1852e62e72f6e0cfeeb63.zip
cleanup old datafile when loading a new one
-rw-r--r--src/engine/shared/datafile.cpp27
1 files changed, 15 insertions, 12 deletions
diff --git a/src/engine/shared/datafile.cpp b/src/engine/shared/datafile.cpp
index 04a867be..4ef278cd 100644
--- a/src/engine/shared/datafile.cpp
+++ b/src/engine/shared/datafile.cpp
@@ -132,28 +132,31 @@ bool CDataFileReader::Open(class IStorage *pStorage, const char *pFilename)
 	AllocSize += sizeof(CDatafile); // add space for info structure
 	AllocSize += Header.m_NumRawData*sizeof(void*); // add space for data pointers
 
-	m_pDataFile = (CDatafile*)mem_alloc(AllocSize, 1);
-	m_pDataFile->m_Header = Header;
-	m_pDataFile->m_DataStartOffset = sizeof(CDatafileHeader) + Size;
-	m_pDataFile->m_ppDataPtrs = (char**)(m_pDataFile+1);
-	m_pDataFile->m_pData = (char *)(m_pDataFile+1)+Header.m_NumRawData*sizeof(char *);
-	m_pDataFile->m_File = File;
-	m_pDataFile->m_Crc = Crc;
+	CDatafile *pTmpDataFile = (CDatafile*)mem_alloc(AllocSize, 1);
+	pTmpDataFile->m_Header = Header;
+	pTmpDataFile->m_DataStartOffset = sizeof(CDatafileHeader) + Size;
+	pTmpDataFile->m_ppDataPtrs = (char**)(pTmpDataFile+1);
+	pTmpDataFile->m_pData = (char *)(pTmpDataFile+1)+Header.m_NumRawData*sizeof(char *);
+	pTmpDataFile->m_File = File;
+	pTmpDataFile->m_Crc = Crc;
 	
 	// clear the data pointers
-	mem_zero(m_pDataFile->m_ppDataPtrs, Header.m_NumRawData*sizeof(void*));
+	mem_zero(pTmpDataFile->m_ppDataPtrs, Header.m_NumRawData*sizeof(void*));
 	
 	// read types, offsets, sizes and item data
-	unsigned ReadSize = io_read(File, m_pDataFile->m_pData, Size);
+	unsigned ReadSize = io_read(File, pTmpDataFile->m_pData, Size);
 	if(ReadSize != Size)
 	{
-		io_close(m_pDataFile->m_File);
-		mem_free(m_pDataFile);
-		m_pDataFile = 0;
+		io_close(pTmpDataFile->m_File);
+		mem_free(pTmpDataFile);
+		pTmpDataFile = 0;
 		dbg_msg("datafile", "couldn't load the whole thing, wanted=%d got=%d", Size, ReadSize);
 		return false;
 	}
 
+	Close();
+	m_pDataFile = pTmpDataFile;
+
 #if defined(CONF_ARCH_ENDIAN_BIG)
 	swap_endian(m_pDataFile->m_pData, sizeof(int), min(Header.m_Swaplen, Size) / sizeof(int));
 #endif