Use asprintf and use free instead of g_free (fixes #2252)