Switch CreateMethodDescriptor over to std::string to avoid memory leaks.

Change-Id: I17005504ab7d055e0750cf35ab2426e0f798f895
diff --git a/src/class_linker.cc b/src/class_linker.cc
index e3912d1..e0461be 100644
--- a/src/class_linker.cc
+++ b/src/class_linker.cc
@@ -736,9 +736,8 @@
   dst->name_ = ResolveString(dex_file, method_id.name_idx_, klass->GetDexCache());
   {
     int32_t utf16_length;
-    scoped_array<char> utf8(dex_file.CreateMethodDescriptor(method_id.proto_idx_,
-                                                          &utf16_length));
-    dst->signature_ = String::AllocFromModifiedUtf8(utf16_length, utf8.get());
+    std::string utf8(dex_file.CreateMethodDescriptor(method_id.proto_idx_, &utf16_length));
+    dst->signature_ = String::AllocFromModifiedUtf8(utf16_length, utf8.c_str());
   }
   dst->proto_idx_ = method_id.proto_idx_;
   dst->code_off_ = src.code_off_;
@@ -1878,7 +1877,7 @@
   }
 
   const char* name = dex_file.dexStringById(method_id.name_idx_);
-  const char* signature = dex_file.CreateMethodDescriptor(method_id.proto_idx_, NULL);
+  std::string signature(dex_file.CreateMethodDescriptor(method_id.proto_idx_, NULL));
   if (is_direct) {
     resolved = klass->FindDirectMethod(name, signature);
   } else {
diff --git a/src/dex_file.cc b/src/dex_file.cc
index f7dec88..3489086 100644
--- a/src/dex_file.cc
+++ b/src/dex_file.cc
@@ -381,8 +381,8 @@
 // Materializes the method descriptor for a method prototype.  Method
 // descriptors are not stored directly in the dex file.  Instead, one
 // must assemble the descriptor from references in the prototype.
-char* DexFile::CreateMethodDescriptor(uint32_t proto_idx,
-                                      int32_t* unicode_length) const {
+std::string DexFile::CreateMethodDescriptor(uint32_t proto_idx,
+    int32_t* unicode_length) const {
   const ProtoId& proto_id = GetProtoId(proto_idx);
   std::string descriptor;
   descriptor.push_back('(');
@@ -404,13 +404,10 @@
   int32_t return_type_length;
   const char* name = dexStringByTypeIdx(return_type_idx, &return_type_length);
   descriptor.append(name);
-  // TODO: should this just return a std::string?
-  scoped_ptr<char> c_string(new char[descriptor.size() + 1]);
-  strcpy(c_string.get(), descriptor.c_str());
   if (unicode_length != NULL) {
     *unicode_length = parameter_length + return_type_length + 2;  // 2 for ( and )
   }
-  return c_string.release();
+  return descriptor;
 }
 
 // Read a signed integer.  "zwidth" is the zero-based byte count.
diff --git a/src/dex_file.h b/src/dex_file.h
index d1125df..76b9c2f 100644
--- a/src/dex_file.h
+++ b/src/dex_file.h
@@ -4,6 +4,7 @@
 #define ART_SRC_DEX_FILE_H_
 
 #include <map>
+#include <string>
 #include <vector>
 
 #include "globals.h"
@@ -482,8 +483,7 @@
     }
   }
 
-  char* CreateMethodDescriptor(uint32_t proto_idx,
-                               int32_t* unicode_length) const;
+  std::string CreateMethodDescriptor(uint32_t proto_idx, int32_t* unicode_length) const;
 
   const byte* GetEncodedArray(const ClassDef& class_def) const {
     if (class_def.static_values_off_ == 0) {
diff --git a/src/dex_file_test.cc b/src/dex_file_test.cc
index 9eb5f73..37c601c 100644
--- a/src/dex_file_test.cc
+++ b/src/dex_file_test.cc
@@ -79,9 +79,8 @@
     const char* name = raw->dexStringById(method_id.name_idx_);
     ASSERT_STREQ("<init>", name);
     int32_t length;
-    scoped_array<const char> descriptor(raw->CreateMethodDescriptor(proto_idx,
-        &length));
-    ASSERT_STREQ("()V", descriptor.get());
+    std::string descriptor(raw->CreateMethodDescriptor(proto_idx, &length));
+    ASSERT_EQ("()V", descriptor);
   }
 
   // Check both virtual methods.
@@ -98,9 +97,8 @@
 
     uint32_t proto_idx = method_id.proto_idx_;
     int32_t length;
-    scoped_array<const char> descriptor(raw->CreateMethodDescriptor(proto_idx,
-        &length));
-    ASSERT_STREQ("(IDJLjava/lang/Object;)Ljava/lang/Float;", descriptor.get());
+    std::string descriptor(raw->CreateMethodDescriptor(proto_idx, &length));
+    ASSERT_EQ("(IDJLjava/lang/Object;)Ljava/lang/Float;", descriptor);
   }
 
   {
@@ -113,9 +111,8 @@
 
     uint32_t proto_idx = method_id.proto_idx_;
     int32_t length;
-    scoped_array<const char> descriptor(raw->CreateMethodDescriptor(proto_idx,
-        &length));
-    ASSERT_STREQ("(ZSC)LCreateMethodDescriptor;", descriptor.get());
+    std::string descriptor(raw->CreateMethodDescriptor(proto_idx, &length));
+    ASSERT_EQ("(ZSC)LCreateMethodDescriptor;", descriptor);
   }
 
 }