Hello there. I got my class Error. For some reason
VS:Community 2017 gives me an error.
HEAP CORRUPTION DETECTED: after Normal block (#149) at 0x00000282A99E0DE0.
CRT detected that the application wrote to memory after end of heap buffer
I've tried to change my code as much as I could and I can't see what's causing the problem.
Here's my code.
Error.h
#ifndef ICT_ERROR_H_
#define ICT_ERROR_H_
#include <iostream>
namespace ict {
class Error {
char* m_message;
public:
Error();
Error(const char* errorMessage);
Error(const Error& em) = delete;
Error& operator=(const Error& em) = delete;
void operator=(const char* errorMessage);
virtual ~Error(){ delete [] m_message; m_message = nullptr;};
void clear();
bool isClear()const;
void message(const char* value);
operator const char*() const;
operator bool()const;
friend std::ostream& operator<<(std::ostream& ostr, const Error& em);
};
std::ostream& operator<<(std::ostream& ostr, const Error& em);
}
#endif //ICT_ERROR_H_
Error.cpp
#define _CRT_SECURE_NO_WARNINGS
#include "Error.h"
#include <iostream>
#include <cstring>
namespace ict{
Error::Error() {
m_message = nullptr;
}
Error::Error(const char* errorMessage) {
m_message = nullptr;
message(errorMessage);
}
void Error::operator=(const char* errorMessage) {
clear();
message(errorMessage);
}
void Error::clear() {
delete[] m_message;
m_message = nullptr;
}
bool Error::isClear()const {
bool clear = false;
if (m_message == nullptr) {
clear = true;
}
return clear;
}
void Error::message(const char* value) {
delete[] m_message;
m_message = new char[strlen(value + 1)];
strcpy(m_message, value);
}
Error::operator const char*() const {
return m_message;
}
Error::operator bool()const {
return isClear();
}
std::ostream& operator<<(std::ostream& ostr, const Error& em) {
if (em.isClear()) {
}
else {
ostr << em.m_message;
}
return ostr;
}
}
Main.cpp
#include <iostream>
#include "Error.h"
using namespace std;
using namespace ict;
int main(){
Error T("Testing Error Message");
Error e;
bool ok = true;
int pc = 1;
cout << T << endl << e << endl << "isClear(): " << (e.isClear() ? "Passed!" : "failed!") << endl;
if (!e.isClear()) ok = false;
for (int i = 0; i < 10000000; i++){
e = "Some error message that is really long long long long long long long long"
" long long long long long long long long long long long long long"
" long long long long long long long long long long long long long"
" long long long long long long long long long long long long long"
" long long long long long long long long long long long long long"
" long long long long long long long long long long long long long"
;
if (i % 100000 == 0) cout << "\r" << pc++ << "%";
}
cout << endl << e << endl << "isClear(): " << (e ? "Failed!" : "Passed!") << endl;
if (e) ok = false;
e.message("Short Error Message");
cout << e << " is the same as " << (const char*)e << endl << "isClear(): " << (e.isClear() ? "Failed!" : "Passed!") << endl;
e.clear();
cout << "Nothing here: >" << e << "<" << endl << "isClear(): " << (e.isClear() ? "Passed!" : "Failed!") << endl;
if (!e) ok = false;
cout << (ok ? "All tests passed!" : "Failed!") << endl;
return 0;
}
What I have tried:
I've tried to compile the same code in Linux terminal with g++ and it works there as expected! So, I assume I got a memory leak some where. Could someone please point me where to look?
Linux terminal output:
Testing Error Message
isClear(): Passed!
100%
Some error message that is really long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long
isClear(): Passed!
Short Error Message is the same as Short Error Message
isClear(): Passed!
Nothing here: ><
isClear(): Passed!
All tests passed!