Crash diagnostics

Jeff Mitchell mitchell at kde.org
Fri Mar 20 18:56:33 CET 2009


Andrew Turner wrote:
> There are a few things in that code that make me a little scared. It's
> been a while since I've touched C-strings though, so I could be wrong,
> but:
> 
> 1) To free memory allocated by "strdup", you should use "free", not
> "delete". This is because it uses "malloc" and not "new" to allocate
> the memory.
> 
> 2) There are assignments from string literals in the unicodeTest
> function and yet later there will be a "delete" call in the
> destructor. You shouldn't free/delete string literals. All you do when
> you assign a string literal is copy the address of the literal. The
> literal itself exists in the executable you're running - it is not
> created separately on the heap - so if you try to delete it then bad
> things happen. Deleting your own executable (or parts of it) is
> probably a good reason to crash.
> 
> 3) You're right about the potential memory leak by doing the strdup
> and not bothering to free it before it's overwritten with a new value,
> Jeff.
> 
> Is there any reason why this code would not work just as well if the
> original strdup is removed so that it is just set to point to an empty
> string literal ( "" ). Then you could remove the call to delete in the
> destructor; with just string literals, nothing ever needs freeing.
> 
> -andrewt512

Personally, I'm trying to figure out why not to replace all of that crap
with a QString on the stack, and never worry about it again.

I think I'll do so, and then poke kde-core-devel and see what they think
of the patch.

Thanks for the input, it's very helpful,
Jeff


-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 195 bytes
Desc: OpenPGP digital signature
Url : http://mail.kde.org/pipermail/amarok-devel/attachments/20090320/082a6d41/attachment.sig 


More information about the Amarok-devel mailing list