core: Use g_clear_pointer/object when possible

Reduces the number of lines and makes the code more readable.