Fixes memory leaks when creating objects for the dictionary

Thanks James Chen