#### Archived

This topic is now archived and is closed to further replies.

# Screwy Assertion Failures

This topic is 5581 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey everybody, I''m far from an expert when it comes to messing with dynamic memory so I was hoping that I may be able to get a little help with a problem I''m having. The following code snippet is where the problem is occuring and I am recieving errors when running a debug build along the lines of: - DAMAGE: after normal block (#45) at 0x008B1DF0 - Program: Mithril.exe File: dbgheap.c Line: 1044 Expression: _CrtIsValidHeapPointer(pUserData) m_rawTime, m_timeInfo and m_LogFile are all defined in the class definiton.

//-------------------------------------------------------------------------------------------------

// FUNCTION: CSysLog()

// DESCRIPTION: Default Constructor. Will generate a logfile using the application''s filename as

//				a base.

//-------------------------------------------------------------------------------------------------

CSysLog::CSysLog()
{
// Store the length of the filename string

char szTempPath[1024];
DWORD dwFilenameStringLength = strlen(szTempPath);

// Get the filename of the program

// Note: This function returns the full path for the filename

//		 so it will be necessary to strip the path from the file

DWORD dwFilenameLength = GetModuleFileName(NULL, szTempPath, dwFilenameStringLength);

// Copy the file path into a string

char *szFilePath = new char[dwFilenameLength];
szFilePath = szTempPath;

// Find the last occurance of ''\'' in the string

char *lastBackslashPointer = strrchr(szFilePath, ''\\'');
int lastBackslash = lastBackslashPointer - szFilePath + 1;

// Find out how long the filename is

int filenameLength = strlen(szFilePath) - lastBackslash;

// Create storage for the filename

char *szFilename = new char[filenameLength];

// Copy the filename from the FilePath

for (int i = 0; i <= filenameLength; i++)
{
szFilename[i] = szFilePath[i + lastBackslash];
}

// Replace the last three characters with "log"

szFilename[filenameLength - 3] = ''l'';
szFilename[filenameLength - 2] = ''o'';
szFilename[filenameLength - 1] = ''g'';

// So after all of that torture, open the file

// Get Current System time

time(&m_rawTime);

// Format the system into somthing useable

m_timeInfo = localtime(&m_rawTime);

// Attempt to open the log file

m_logFile.open(szFilename, std::ios::out);

// If the file doesn''t open let the user know

if (!m_logFile.is_open())
{
char szErrorBuffer[256];
sprintf(szErrorBuffer, "Failed to open logfile: %s\nNo log output will be available for this logfile.", szFilename);
MessageBox(NULL, szErrorBuffer, "LOG ERROR", MB_OK | MB_TOPMOST | MB_ICONERROR);
}

m_logFile << "                                        LOG FILE OPENED\n";

// Format a log file header

sprintf(szLogFileHeader, "                     %s created successfully at %d:%d:%d on %d/%d/%d\n", szFilename, m_timeInfo->tm_hour, m_timeInfo->tm_min, m_timeInfo->tm_sec, m_timeInfo->tm_mday, m_timeInfo->tm_mon + 1, m_timeInfo->tm_year + 1900);

m_logFile << "====================================================================================================\n\n";

// Write all this to the file

m_logFile << std::flush;

// Clean up

delete[] szFilename;
delete[] szFilePath;
}


Hope that helps a bit. What seems strange to me is that it runs just fine as long as I do not include the pair of delete[] at the end. ------------------------------------------------------------ I am a signature virus. Please add me to your signature so that I may multiply.

##### Share on other sites
char szTempPath[1024];...szFilePath = szTempPath;...delete[] szFilePath;

You''re trying to deallocate memory you did not allocate yourself. And you have a memory leak (you throw away the pointer to the memory you allocated with new char[dwFilenameLength] so there''s no way to refer to it and deallocate it.

##### Share on other sites
The reason why delete is dying is that you are trying to delete a non-dynamic variable (on the stack). What you are doing wrong is just after your first new.

// Copy the file path into a stringchar *szFilePath = new char[dwFilenameLength];szFilePath = szTempPath;

In the second statement you didn''t copy the string (as I assume you intended) but infact you changed the pointer so that it points to the start of ''szTempPath''. Try doing this:

// Copy the file path into a stringchar *szFilePath = new char[dwFilenameLength+1];strcpy(szFilePath, szTempPath);

Note: you need an extra byte after the length of the string to fit in the null terminator!

BTW: you could also run into trouble on your GetModuleFileName call; doing strlen(szTempPath) is meaningless until it has been initialised with a string - you can NEVER get the length of an array this way. What you should do is this:
  CSysLog::CSysLog()  {	// Store the length of the filename string	  char szTempPath[MAX_PATH];  DWORD dwFilenameLength = GetModuleFileName(NULL, szTempPath, MAX_PATH);...}

(Note MAX_PATH is a #define in the windows header files)

##### Share on other sites

Thanks to the both of you. That was very informative. Everything is working like a charm now.

------------------------------------------------------------

• 17
• 10
• 19
• 14
• 19