replax 0 Newbie Poster

Hey guys,

after compiling my program I get the following error

Windows has triggered a breakpoint in *.exe.
This may be due to a corruption of the heap, which indicates a bug in *.exe or any of the DLLs it has loaded.
This may also be due to the user pressing F12 while *.exe has focus.

so I debugged my code and I narrowed the error down to the last line of code that i posted below. it is

delete[] pchMskFileName;

. so I understand that you would get that kind of error when you allocate memory with "new", then assign e.g. "hello" to the pointer that actually pointed to the allocated memory and you then try to "delete[]" that memory. because your pointer now points to another memory address, your heap will corrupt. however, i cannot detect that flaw in my code.

using namespace std;
	int nMskFileNameLength = (strlen(m_pchFileName) / sizeof(*m_pchFileName));
	char *pchMskFileName = new char[nMskFileNameLength];
	int nMskCopyLoopLength = nMskFileNameLength - 3;
	for(int i = 0; i <= nMskCopyLoopLength; ++i)
	{
		pchMskFileName[i] = m_pchFileName[i];
	}
	pchMskFileName[nMskFileNameLength - 3] = 'm';
	pchMskFileName[nMskFileNameLength - 2] = 's';
	pchMskFileName[nMskFileNameLength - 1] = 'k';
	ifstream mskin(pchMskFileName);
	if(!mskin)
	{
		bool couldntOpenSpriteMaskForReading = false;
		assert(couldntOpenSpriteMaskForReading);
		return false;
	}
	int nCount = SpriteWidth(m_nSID) * SpriteHeight(m_nSID);
	m_vctSpriteMask.reserve(nCount);
	int nIn;
	for(int i = 0; i < nCount; ++i)
	{
		mskin >> nIn;
		m_vctSpriteMask.push_back(nIn);
	}
	delete[] pchMskFileName;

has anyone got any idea what might be causing the error in my code?

thanks a lot!

cheers!

EDIT: FOUND THE ERROR, i forget to leave space for a null terminator!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.