[vdr] Re: cString operator= with same buffer

Jon Burgess jburgess at uklinux.net
Sun Nov 20 18:17:19 CET 2005


Holger Brunn wrote:
>  cString &cString::operator=(const cString &String)
>  {
> -  free(s);
> +  if(s!=String.s)
> +  {
> +    free(s);
> +  }
>    s = String.s ? strdup(String.s) : NULL;
>    return *this;
>  }

Doesn't this cause a memory leak? It'll strdup() the old string and then 
lose the old pointer to it. Looks to me like it should instead be:

   if(s!=String.s) {
     free(s);
     s = String.s ? strdup(String.s) : NULL;
   }

or maybe something like:
	if (&String == this) return *this;

	Jon



More information about the vdr mailing list