Fix memory leaks in the CFI tests.

Change-Id: Icb98e4995731c7ac5f99d1be20b447161ea4c4bd
diff --git a/compiler/dex/quick/quick_cfi_test.cc b/compiler/dex/quick/quick_cfi_test.cc
index 0540a8c..2e62166 100644
--- a/compiler/dex/quick/quick_cfi_test.cc
+++ b/compiler/dex/quick/quick_cfi_test.cc
@@ -84,7 +84,7 @@
     cu.mir_graph->current_code_item_ = &code_item;
 
     // Generate empty method with some spills.
-    Mir2Lir* m2l = QuickCompiler::GetCodeGenerator(&cu, NULL);
+    std::unique_ptr<Mir2Lir> m2l(QuickCompiler::GetCodeGenerator(&cu, nullptr));
     m2l->frame_size_ = 64u;
     m2l->CompilerInitializeRegAlloc();
     for (const auto& info : m2l->reg_pool_->core_regs_) {
diff --git a/compiler/optimizing/optimizing_cfi_test.cc b/compiler/optimizing/optimizing_cfi_test.cc
index 8602255..6d986ba 100644
--- a/compiler/optimizing/optimizing_cfi_test.cc
+++ b/compiler/optimizing/optimizing_cfi_test.cc
@@ -47,7 +47,8 @@
     isa_features.reset(InstructionSetFeatures::FromVariant(isa, "default", &error));
     HGraph graph(&allocator);
     // Generate simple frame with some spills.
-    auto code_gen = CodeGenerator::Create(&graph, isa, *isa_features.get(), opts);
+    std::unique_ptr<CodeGenerator> code_gen(
+        CodeGenerator::Create(&graph, isa, *isa_features.get(), opts));
     const int frame_size = 64;
     int core_reg = 0;
     int fp_reg = 0;
@@ -74,10 +75,10 @@
     code_gen->GenerateFrameEntry();
     code_gen->GetInstructionVisitor()->VisitReturnVoid(new (&allocator) HReturnVoid());
     // Get the outputs.
+    InternalCodeAllocator code_allocator;
+    code_gen->Finalize(&code_allocator);
+    const std::vector<uint8_t>& actual_asm = code_allocator.GetMemory();
     Assembler* opt_asm = code_gen->GetAssembler();
-    std::vector<uint8_t> actual_asm(opt_asm->CodeSize());
-    MemoryRegion code(&actual_asm[0], actual_asm.size());
-    opt_asm->FinalizeInstructions(code);
     const std::vector<uint8_t>& actual_cfi = *(opt_asm->cfi().data());
 
     if (kGenerateExpected) {
@@ -87,6 +88,24 @@
       EXPECT_EQ(expected_cfi, actual_cfi);
     }
   }
+
+ private:
+  class InternalCodeAllocator : public CodeAllocator {
+   public:
+    InternalCodeAllocator() {}
+
+    virtual uint8_t* Allocate(size_t size) {
+      memory_.resize(size);
+      return memory_.data();
+    }
+
+    const std::vector<uint8_t>& GetMemory() { return memory_; }
+
+   private:
+    std::vector<uint8_t> memory_;
+
+    DISALLOW_COPY_AND_ASSIGN(InternalCodeAllocator);
+  };
 };
 
 #define TEST_ISA(isa) \