matrix works but memory leak and aliasing