Improve mag_conn memory handling