c++ memory problems

tschumann

Spy
Joined
May 31, 2008
Messages
838
Reaction score
0
I'm writing a program that opens up a Half-Life .bsp file, but I'm having some problems with memory. In Release the program runs fine and, but in Debug I get the following error:

Debug assertion failed!
File: <...>\dbgdel.cpp
Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)

It seems to appear when I delete the instance of my bsp class.
Anyone know what the problem might be?
 
post your bsp class destructor. This can occur when you attempt to delete an invalid piece of memory, for example deleting the same piece of memory twice.
 
Okay; here's the destructor and the function that does all the memory allocation:
Code:
        bsp::~bsp()
	{
		delete [] m_Entities.szEntityData;
		m_Entities.szEntityData = NULL;
		delete [] m_TextureLump.iTextureOffset;
		m_TextureLump.iTextureOffset = NULL;
		delete [] m_pTextureData;
		m_pTextureData = NULL;
		fclose( pBSP );
	}

	bool bsp::ReadFile( char *szFilename )
	{
		fopen_s( &pBSP, szFilename, "rb" );
		if( !pBSP )
		{
			return false;
		}
		fread( &m_Header, sizeof(bspHeader), 1, pBSP );
		if( m_Header.iVersion != 29 && m_Header.iVersion != 30 )
		{
			return false;
		}

		this->strFilename = GetFileName( szFilename );

		if( m_Header.Lumps[0].iLumpOffset > m_Header.Lumps[1].iLumpOffset )
		{
			bIsBlueShift = false;
		}
		else if( m_Header.Lumps[0].iLumpOffset < m_Header.Lumps[1].iLumpOffset )
		{
			bIsBlueShift = true;
		}

		// Lump 0
		if( bIsBlueShift == true )
		{		
			fseek( pBSP, m_Header.Lumps[BSHIFT_LUMP_ENTITIES].iLumpOffset, SEEK_SET );
			m_Entities.szEntityData = new char[m_Header.Lumps[BSHIFT_LUMP_ENTITIES].iLumpSize];
			fread( m_Entities.szEntityData, m_Header.Lumps[BSHIFT_LUMP_ENTITIES].iLumpSize, 1, pBSP );
		}
		else if( bIsBlueShift == false )
		{
			fseek( pBSP, m_Header.Lumps[VALVE_LUMP_ENTITIES].iLumpOffset, SEEK_SET );
			m_Entities.szEntityData = new char[m_Header.Lumps[VALVE_LUMP_ENTITIES].iLumpSize];
			fread( m_Entities.szEntityData, m_Header.Lumps[VALVE_LUMP_ENTITIES].iLumpSize, 1, pBSP );
		}

		// Lump 2
		fseek( pBSP, m_Header.Lumps[VALVE_LUMP_TEXTURES].iLumpOffset, SEEK_SET );
		fread( &m_TextureLump.iTextureCount, sizeof(m_TextureLump.iTextureCount), 1, pBSP );
		m_TextureLump.iTextureOffset = new int[m_TextureLump.iTextureCount];
		fread( m_TextureLump.iTextureOffset, sizeof(__int32), m_TextureLump.iTextureCount, pBSP );

		m_pTextureData = new miptextureData[m_TextureLump.iTextureCount];
		for( int i = 0; i < m_TextureLump.iTextureCount; i++ )
		{
			fseek( pBSP, m_Header.Lumps[VALVE_LUMP_TEXTURES].iLumpOffset, SEEK_SET );
			fseek( pBSP, m_TextureLump.iTextureOffset[i], SEEK_CUR );
			fread( &m_pTextureData[i], sizeof(miptextureData), 1, pBSP );
		}

		return true;
	}
 
I've fixed it somehow. I think that some memory was getting deleted before delete was called.
 
Back
Top