[vdr] Re: cString operator= with same buffer
Holger Brunn
holger.brunn at stud.uni-karlsruhe.de
Sun Nov 20 18:28:13 CET 2005
Jon Burgess on Sunday 20 November 2005 18:17:
> Doesn't this cause a memory leak? It'll strdup() the old string and then
> lose the old pointer to it.
Hello,
as far as I can see, there wouldn't be a mem leak - as both cStrings point to
the same buffer, you only loose one of them. The one that was assigned from
still has a pointer to the original buffer (and its destructor will free it).
>
> if(s!=String.s) {
> free(s);
> s = String.s ? strdup(String.s) : NULL;
> }
>
> or maybe something like:
> if (&String == this) return *this;
>
This is possibility 1) from my original posting I asked for arguments for. As
stated there, I'd prefer making a new copy as I expect operator= to do so in
every case.
Greetings
Holger
More information about the vdr
mailing list