tp: fix memory leak of table objects

This CL adds the missing virtual destructor which was cauisng memory
leaks as dynamic tables which were upcast to Table would not be
correctly destroyed when the SQLite cursor was destroyed.

Change-Id: I56bc3d0b078267393b71450a1b644847a39c0a0b
diff --git a/Android.bp b/Android.bp
index d601896..e1931e2 100644
--- a/Android.bp
+++ b/Android.bp
@@ -6426,6 +6426,9 @@
 // GN: //src/trace_processor/tables:tables
 filegroup {
   name: "perfetto_src_trace_processor_tables_tables",
+  srcs: [
+    "src/trace_processor/tables/table_destructors.cc",
+  ],
 }
 
 // GN: //src/trace_processor/tables:unittests
diff --git a/BUILD b/BUILD
index fcb16ab..042d95a 100644
--- a/BUILD
+++ b/BUILD
@@ -813,6 +813,7 @@
         "src/trace_processor/tables/metadata_tables.h",
         "src/trace_processor/tables/profiler_tables.h",
         "src/trace_processor/tables/slice_tables.h",
+        "src/trace_processor/tables/table_destructors.cc",
         "src/trace_processor/tables/track_tables.h",
     ],
 )
diff --git a/src/trace_processor/db/table.cc b/src/trace_processor/db/table.cc
index fcfc52a..ebcdf9e 100644
--- a/src/trace_processor/db/table.cc
+++ b/src/trace_processor/db/table.cc
@@ -20,6 +20,7 @@
 namespace trace_processor {
 
 Table::Table() = default;
+Table::~Table() = default;
 
 Table::Table(StringPool* pool, const Table* parent) : string_pool_(pool) {
   if (!parent)
diff --git a/src/trace_processor/db/table.h b/src/trace_processor/db/table.h
index b521d47..94d8cfc 100644
--- a/src/trace_processor/db/table.h
+++ b/src/trace_processor/db/table.h
@@ -88,6 +88,7 @@
   };
 
   Table();
+  virtual ~Table();
 
   // We explicitly define the move constructor here because we need to update
   // the Table pointer in each column in the table.
diff --git a/src/trace_processor/tables/BUILD.gn b/src/trace_processor/tables/BUILD.gn
index 83628fa..7f6ac2b 100644
--- a/src/trace_processor/tables/BUILD.gn
+++ b/src/trace_processor/tables/BUILD.gn
@@ -23,6 +23,7 @@
     "metadata_tables.h",
     "profiler_tables.h",
     "slice_tables.h",
+    "table_destructors.cc",
     "track_tables.h",
   ]
   deps = [
diff --git a/src/trace_processor/tables/macros_benchmark.cc b/src/trace_processor/tables/macros_benchmark.cc
index 5e76fc6..01bc9f0 100644
--- a/src/trace_processor/tables/macros_benchmark.cc
+++ b/src/trace_processor/tables/macros_benchmark.cc
@@ -41,6 +41,9 @@
 
 PERFETTO_TP_TABLE(PERFETTO_TP_CHILD_TABLE);
 
+RootTestTable::~RootTestTable() = default;
+ChildTestTable::~ChildTestTable() = default;
+
 }  // namespace
 }  // namespace trace_processor
 }  // namespace perfetto
diff --git a/src/trace_processor/tables/macros_internal.h b/src/trace_processor/tables/macros_internal.h
index 60b41c7..35bbec8 100644
--- a/src/trace_processor/tables/macros_internal.h
+++ b/src/trace_processor/tables/macros_internal.h
@@ -93,6 +93,7 @@
                  static_cast<uint32_t>(row_maps_.size()) - 1));
     }
   }
+  ~MacroTable() override;
 
   const char* table_name() const { return name_; }
 
@@ -342,6 +343,7 @@
        */                                                                     \
       PERFETTO_TP_TABLE_COLUMNS(DEF, PERFETTO_TP_TABLE_CONSTRUCTOR_COLUMN);   \
     }                                                                         \
+    ~class_name() override;                                                   \
                                                                               \
     IdAndRow Insert(const Row& row) {                                         \
       Id id;                                                                  \
diff --git a/src/trace_processor/tables/macros_unittest.cc b/src/trace_processor/tables/macros_unittest.cc
index 1ecb36f..c53096c 100644
--- a/src/trace_processor/tables/macros_unittest.cc
+++ b/src/trace_processor/tables/macros_unittest.cc
@@ -50,6 +50,11 @@
   C(StringPool::Id, end_state)
 PERFETTO_TP_TABLE(PERFETTO_TP_TEST_CPU_SLICE_TABLE_DEF);
 
+TestEventTable::~TestEventTable() = default;
+TestCounterTable::~TestCounterTable() = default;
+TestSliceTable::~TestSliceTable() = default;
+TestCpuSliceTable::~TestCpuSliceTable() = default;
+
 class TableMacrosUnittest : public ::testing::Test {
  protected:
   StringPool pool_;
diff --git a/src/trace_processor/tables/table_destructors.cc b/src/trace_processor/tables/table_destructors.cc
new file mode 100644
index 0000000..dc1e8a5
--- /dev/null
+++ b/src/trace_processor/tables/table_destructors.cc
@@ -0,0 +1,78 @@
+/*
+ * Copyright (C) 2020 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "src/trace_processor/tables/android_tables.h"
+#include "src/trace_processor/tables/counter_tables.h"
+#include "src/trace_processor/tables/metadata_tables.h"
+#include "src/trace_processor/tables/profiler_tables.h"
+#include "src/trace_processor/tables/slice_tables.h"
+#include "src/trace_processor/tables/track_tables.h"
+
+namespace perfetto {
+namespace trace_processor {
+namespace macros_internal {
+// macros_internal.h
+MacroTable::~MacroTable() = default;
+}  // namespace macros_internal
+
+namespace tables {
+// android_tables.h
+AndroidLogTable::~AndroidLogTable() = default;
+
+// counter_tables.h
+CounterTable::~CounterTable() = default;
+
+// metadata_tables.h
+RawTable::~RawTable() = default;
+ArgTable::~ArgTable() = default;
+MetadataTable::~MetadataTable() = default;
+ThreadTable::~ThreadTable() = default;
+ProcessTable::~ProcessTable() = default;
+
+// profiler_tables.h
+StackProfileMappingTable::~StackProfileMappingTable() = default;
+StackProfileFrameTable::~StackProfileFrameTable() = default;
+StackProfileCallsiteTable::~StackProfileCallsiteTable() = default;
+CpuProfileStackSampleTable::~CpuProfileStackSampleTable() = default;
+SymbolTable::~SymbolTable() = default;
+HeapProfileAllocationTable::~HeapProfileAllocationTable() = default;
+ExperimentalFlamegraphNodesTable::~ExperimentalFlamegraphNodesTable() = default;
+HeapGraphObjectTable::~HeapGraphObjectTable() = default;
+HeapGraphReferenceTable::~HeapGraphReferenceTable() = default;
+VulkanMemoryAllocationsTable::~VulkanMemoryAllocationsTable() = default;
+
+// slice_tables.h
+SliceTable::~SliceTable() = default;
+InstantTable::~InstantTable() = default;
+SchedSliceTable::~SchedSliceTable() = default;
+GpuSliceTable::~GpuSliceTable() = default;
+
+// track_tables.h
+TrackTable::~TrackTable() = default;
+ProcessTrackTable::~ProcessTrackTable() = default;
+ThreadTrackTable::~ThreadTrackTable() = default;
+GpuTrackTable::~GpuTrackTable() = default;
+CounterTrackTable::~CounterTrackTable() = default;
+ThreadCounterTrackTable::~ThreadCounterTrackTable() = default;
+ProcessCounterTrackTable::~ProcessCounterTrackTable() = default;
+CpuCounterTrackTable::~CpuCounterTrackTable() = default;
+IrqCounterTrackTable::~IrqCounterTrackTable() = default;
+SoftirqCounterTrackTable::~SoftirqCounterTrackTable() = default;
+GpuCounterTrackTable::~GpuCounterTrackTable() = default;
+}  // namespace tables
+
+}  // namespace trace_processor
+}  // namespace perfetto