Jon Burgess on Sunday 20 November 2005 20:01:
In the normal case you'd be correct, the destructor would be called twice and free up both, but in the case that the cStrings are equal the destructor will only be called once on the new cString. There is nothing which will destruct the old cString. I wrote the quick test app as attached and valgrind seems to agree with me.
I see, didn't think of this. So we either need also a check for equality of references or just forget about it completely, as both situations would be rather theoretical. So could we agree on
if(&String == this) return *this; if(s!=String.s) { free(s); } s = String.s ? strdup(String.s) : NULL;
This will avoid the memory leak you pointed out and beahve the way you would expect from an assignment operator?
Greetings Holger