restructure handling of object close that is initiated by erlang instead of api.  fixes segfaults