Added shared_ptr-based garbage collection for local references