Wrong use of free() causes core dump