I'm having problems with a destructor. It says my heap is corrupted and after debugging I'm assuming that this is the problem. It is after a certain function so here's that function:
The error occurs after the last constructor function and at delet[]m_buf in the destructor. The actual error is: Heap corruption detected.....detected that the program wrote to the memory after the end of heap buffer....Code:#include "stdafx.h" #include <string> #include <iostream> #include <fstream> #include <stdlib.h> #include "string.h" #define NULL "\0" extern std::ofstream csis; //Default Ctor String::String() { m_buf = new char[1]; m_length = 1; m_buf[0] = '\0'; m_name = ""; } //Ctor with string literal param String::String(char* s) { int inputString = strlen(s); inputString++; m_length = inputString; m_buf = new char[inputString]; strcpy_s(m_buf, m_length, s); m_name = ""; } //Ctor with single char as param String::String(char c) { m_buf = new char[2]; m_buf[0] = c; m_buf[1] = '\0'; int inputString = strlen(m_buf); m_length = inputString; m_name = ""; } String::String(int x) { m_buf = new char[sizeof(x)]; _itoa(x, m_buf, 10); m_length = strlen(m_buf); m_name = ""; } //Ctor with pointer to data member String::String(const String& s) { m_length = s.m_length; m_length; m_buf = new char[m_length]; strcpy_s(m_buf, m_length, s.m_buf); m_name = ""; } //Ctor with repeating char String::String(char c, int x) { m_buf = new char[x]; for(int i =0; i < x; i++) { m_buf[i] = c; } m_buf[x] = '\0'; m_length = strlen(m_buf); m_name = ""; } //Destructor String::~String() { if(m_name) delete[]m_name; if(m_buf) delete[]m_buf; } ......
Any thoughts please and thank you?



LinkBack URL
About LinkBacks


