Re: cString operator= with same buffer

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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


[Index of Archives]     [Linux Media]     [Asterisk]     [DCCP]     [Netdev]     [Xorg]     [Util Linux NG]     [Xfree86]     [Big List of Linux Books]     [Fedora Users]     [Fedora Women]     [ALSA Devel]     [Linux USB]

  Powered by Linux