Fix some memory leaks found in static analysis

Change-Id: Icd630009793c51acfaed45763ef50489ead40024
diff --git a/media/mtp/MtpDevice.cpp b/media/mtp/MtpDevice.cpp
index 4383b5f..fb1b073 100644
--- a/media/mtp/MtpDevice.cpp
+++ b/media/mtp/MtpDevice.cpp
@@ -68,17 +68,29 @@
                 interface->bInterfaceSubClass == 1 && // Still Image Capture
                 interface->bInterfaceProtocol == 1)     // Picture Transfer Protocol (PIMA 15470)
             {
-                LOGD("Found camera: \"%s\" \"%s\"\n", usb_device_get_manufacturer_name(device),
-                        usb_device_get_product_name(device));
+                char* manufacturerName = usb_device_get_manufacturer_name(device);
+                char* productName = usb_device_get_product_name(device);
+                LOGD("Found camera: \"%s\" \"%s\"\n", manufacturerName, productName);
+                free(manufacturerName);
+                free(productName);
             } else if (interface->bInterfaceClass == 0xFF &&
                     interface->bInterfaceSubClass == 0xFF &&
                     interface->bInterfaceProtocol == 0) {
                 char* interfaceName = usb_device_get_string(device, interface->iInterface);
-                if (!interfaceName || strcmp(interfaceName, "MTP"))
+                if (!interfaceName) {
                     continue;
+                } else if (strcmp(interfaceName, "MTP")) {
+                    free(interfaceName);
+                    continue;
+                }
+                free(interfaceName);
+
                 // Looks like an android style MTP device
-                LOGD("Found MTP device: \"%s\" \"%s\"\n", usb_device_get_manufacturer_name(device),
-                        usb_device_get_product_name(device));
+                char* manufacturerName = usb_device_get_manufacturer_name(device);
+                char* productName = usb_device_get_product_name(device);
+                LOGD("Found MTP device: \"%s\" \"%s\"\n", manufacturerName, productName);
+                free(manufacturerName);
+                free(productName);
             } else {
                 // look for special cased devices based on vendor/product ID
                 // we are doing this mainly for testing purposes
@@ -119,6 +131,7 @@
                 ep = (struct usb_endpoint_descriptor *)usb_descriptor_iter_next(&iter);
                 if (!ep || ep->bDescriptorType != USB_DT_ENDPOINT) {
                     LOGE("endpoints not found\n");
+                    usb_device_close(device);
                     return NULL;
                 }
                 if (ep->bmAttributes == USB_ENDPOINT_XFER_BULK) {
@@ -133,11 +146,13 @@
             }
             if (!ep_in_desc || !ep_out_desc || !ep_intr_desc) {
                 LOGE("endpoints not found\n");
+                usb_device_close(device);
                 return NULL;
             }
 
             if (usb_device_claim_interface(device, interface->bInterfaceNumber)) {
                 LOGE("usb_device_claim_interface failed errno: %d\n", errno);
+                usb_device_close(device);
                 return NULL;
             }
 
@@ -220,6 +235,7 @@
                 MtpProperty* property = getDevicePropDesc(propCode);
                 if (property) {
                     property->print();
+                    delete property;
                 }
             }
         }
@@ -236,11 +252,13 @@
                 for (int j = 0; j < props->size(); j++) {
                     MtpObjectProperty prop = (*props)[j];
                     MtpProperty* property = getObjectPropDesc(prop, format);
-                    if (property)
+                    if (property) {
                         property->print();
-                    else
+                        delete property;
+                    } else {
                         LOGE("could not fetch property: %s",
                                 MtpDebug::getObjectPropCodeName(prop));
+                    }
                 }
             }
         }
@@ -476,18 +494,24 @@
 
 MtpObjectHandle MtpDevice::getParent(MtpObjectHandle handle) {
     MtpObjectInfo* info = getObjectInfo(handle);
-    if (info)
-        return info->mParent;
-    else
+    if (info) {
+        MtpObjectHandle parent = info->mParent;
+        delete info;
+        return parent;
+    } else {
         return -1;
+    }
 }
 
 MtpObjectHandle MtpDevice::getStorageID(MtpObjectHandle handle) {
     MtpObjectInfo* info = getObjectInfo(handle);
-    if (info)
-        return info->mStorageID;
-    else
+    if (info) {
+        MtpObjectHandle storageId = info->mStorageID;
+        delete info;
+        return storageId;
+    } else {
         return -1;
+    }
 }
 
 MtpObjectPropertyList* MtpDevice::getObjectPropsSupported(MtpObjectFormat format) {
@@ -668,8 +692,10 @@
         void* initialData = mData.getData(initialDataLength);
         if (initialData) {
             if (initialDataLength > 0) {
-                if (write(fd, initialData, initialDataLength) != initialDataLength)
+                if (write(fd, initialData, initialDataLength) != initialDataLength) {
+                    free(initialData);
                     goto fail;
+                }
                 remaining -= initialDataLength;
             }
             free(initialData);