[vdr] Re: cString operator= with same buffer

Jon Burgess jburgess at uklinux.net
Sun Nov 20 20:53:01 CET 2005


Holger Brunn wrote:
> 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?

What happens if someone was trying to remove the initial part of the 
string, e.g. something like:

	cString hw("Hello World");
	hw = strstr(*hw, " ")+1;

The two buffers wouldn't be equal, but freeing the input before the 
strdup would still be wrong. How about:

   if(&String == this) return *this;
   const char *old = s;
   s = String.s ? strdup(String.s) : NULL;
   free(old);

Does this stll match your expected behaviour?

	Jon



More information about the vdr mailing list