Fix potential memory leaks in mainwindow