Hunting memory leaks in hope to fix Bug #1713

I have just done a manual static analyze of the code, following the path of all malloc, calloc and strdup calls (until I get too bored) and I found some few minor things. Done some testing, everything looks fine, at least it does not crash for me.
But my changes probably needs to be reviewed by someone that's more familiar with the code.