This code contains, at least, two rather obvious problems:
- You allocate data apparently using
new wchar_t[n]
but you release it usingdelete p
rather than usingdelete[] p
. - The likely cause of your problem is that you concatenate two strings into the memory of one string and then allocate enough memory to copy the data over.
You probably want something more along the lines of this:
myObject &operator += (const myObject &s) {
size_t len = wcslen(this->data) + wcslen(s.data);
std::unique_ptr<wchar_t[]> tmp(new wchar_t[len + 1]);
wcscpy(tmp.get(), this->data);
wcscat(tmp.get(), s.data);
delete[] this->data;
this->data = tmp.release();
return *this;
}
Actually, I think you want to use std::wstring
: this class already provides the logic, probably in a more efficient form anyway.