free(resp.data);
        resp.data = NULL;
        json_object_put(resObj);
+       resObj = NULL;
        curl_easy_cleanup(curl);
+       curl = NULL;
 }
 
 void kds_s2000w_handler_close()
 {
-       if (sessionid = 0)
+       if (sessionid == 0)
                return;
 
        CURL *curl = curl_easy_init();
 
        sessionid = 0;
        curl_slist_free_all(headers);
+       headers = NULL;
        curl_easy_cleanup(curl);
+       curl = NULL;
 }
\ No newline at end of file
 
 #include <sane/sane.h>
 #include "kds_s2000w_handler.h"
 
+SANE_Device* device;
+
 SANE_Status _sane_kds_s2000w_net_init(SANE_Int* version_code, SANE_Auth_Callback authorize)
 {
        *version_code = SANE_VERSION_CODE(1, 0, 0);
 {
        printf("kds_s2000w_net exit\n");
        kds_s2000w_handler_close();
+       free(device);
+       device = NULL;
 }
 
 SANE_Status _sane_kds_s2000w_net_get_devices(SANE_Device*** device_list,
        if (local_only)
                return SANE_STATUS_NO_MEM;
 
-       SANE_Device* device = malloc(sizeof(SANE_Device));
+       device = malloc(sizeof(SANE_Device));
        device->name = "kds_s2000w_net";
        device->vendor = "Kodak";
        device->model = "Kodak Alaris s2000w series";
 void _sane_kds_s2000w_net_close(SANE_Handle handle)
 {
        printf("kds_s2000w_net close\n");
+       kds_s2000w_handler_close();
 }
 
 const SANE_Option_Descriptor* _sane_kds_s2000w_net_get_option_descriptor(