Removed memory leak.

PiperOrigin-RevId: 676993763
Change-Id: I7097b9fcf13715278d92ffb1249e02d291e77141
diff --git a/riscv/riscv_renode.cc b/riscv/riscv_renode.cc
index f7381f2..254282a 100644
--- a/riscv/riscv_renode.cc
+++ b/riscv/riscv_renode.cc
@@ -22,7 +22,6 @@
 #include <functional>
 #include <iostream>
 #include <memory>
-#include <new>
 #include <string>
 #include <string_view>
 
@@ -207,14 +206,17 @@
   proto_file_name = absl::StrCat("./mpact_riscv_", name_, ".proto");
   std::fstream proto_file(proto_file_name.c_str(), std::ios_base::out);
   std::string serialized;
-  if (!proto_file.good() || !google::protobuf::TextFormat::PrintToString(
-                                *component_proto.get(), &serialized)) {
-    LOG(ERROR) << "Failed to write proto to file";
+  if (!proto_file.good()) {
+    LOG(ERROR) << "Failed to open proto file for writing";
+  } else if (!google::protobuf::TextFormat::PrintToString(
+                 *component_proto.get(), &serialized)) {
+    LOG(ERROR) << "Failed to serialize protos";
   } else {
     proto_file << serialized;
-    proto_file.close();
   }
+  proto_file.close();
   // Clean up.
+  delete renode_router_;
   delete mem_profiler_;
   delete inst_profiler_;
   delete instrumentation_control_;
diff --git a/riscv/test/librenode_mpact_riscv64_so_test.cc b/riscv/test/librenode_mpact_riscv64_so_test.cc
index ad9827f..9055f70 100644
--- a/riscv/test/librenode_mpact_riscv64_so_test.cc
+++ b/riscv/test/librenode_mpact_riscv64_so_test.cc
@@ -21,9 +21,9 @@
 class LibRenodeMpactRiscV64SoTest : public ::testing::Test {
  protected:
   LibRenodeMpactRiscV64SoTest() {
+    absl::LeakCheckDisabler disabler;
     std::string path = absl::StrCat(kDepotPath, kFileName);
-    absl::LeakCheckDisabler disabler;  // Ignore leaks from dlopen.
-    lib_ = dlopen(path.c_str(), RTLD_LAZY);
+    lib_ = absl::IgnoreLeak(dlopen(path.c_str(), RTLD_LAZY));
     CHECK_NE(lib_, nullptr);
   }
 
@@ -97,6 +97,7 @@
 }
 
 using ConstructType = int32_t (*)(char *, int32_t);
+using DestructType = void (*)(int32_t);
 using StepType = uint64_t (*)(int32_t, uint64_t, int32_t *);
 using SetConfigType = int32_t (*)(int32_t, const char *[], const char *[],
                                   int32_t);
@@ -120,6 +121,8 @@
       reinterpret_cast<GetRegInfo>(dlsym(lib_, "get_reg_info"));
   WriteRegisterType write_register =
       reinterpret_cast<WriteRegisterType>(dlsym(lib_, "write_register"));
+  DestructType destruct =
+      reinterpret_cast<DestructType>(dlsym(lib_, "destruct"));
   // Verify that the function pointers are valid.
   CHECK_NE(construct, nullptr);
   CHECK_NE(step, nullptr);
@@ -174,6 +177,7 @@
     total_stepped += num_stepped;
   } while (num_stepped > 10'000 && status == 0 && total_stepped < 100'000);
   EXPECT_EQ("Hello world! 5\n", testing::internal::GetCapturedStdout());
+  destruct(id);
 }
 
 }  // namespace