From 4e4ed063fc94e5eab4f3d7c387613ec77dfd6f10 Mon Sep 17 00:00:00 2001 From: Mislav Bradac Date: Tue, 26 Sep 2017 16:47:25 +0200 Subject: [PATCH] Fix memory leak in InsertEdge Summary: Few days ago I put a task to reduce number of exceptions in memgraph codebase :) I think we should use exceptions only in modules where we are using smart pointers. Reviewers: florijan, buda Reviewed By: florijan Subscribers: pullbot Differential Revision: https://phabricator.memgraph.io/D834 --- src/database/graph_db_accessor.cpp | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/database/graph_db_accessor.cpp b/src/database/graph_db_accessor.cpp index a0551544e..912c02f26 100644 --- a/src/database/graph_db_accessor.cpp +++ b/src/database/graph_db_accessor.cpp @@ -239,6 +239,10 @@ EdgeAccessor GraphDbAccessor::InsertEdge(VertexAccessor &from, // create an edge auto edge_vlist = new mvcc::VersionList(*transaction_, *from.vlist_, *to.vlist_, edge_type); + // We need to insert edge_vlist to edges_ before calling update since update + // can throw and edge_vlist will not be garbage collected if it is not in + // edges_ skiplist. + bool success = db_.edges_.access().insert(edge_vlist).second; // ensure that the "from" accessor has the latest version from.SwitchNew(); @@ -249,11 +253,10 @@ EdgeAccessor GraphDbAccessor::InsertEdge(VertexAccessor &from, to.SwitchNew(); to.update().in_.emplace(from.vlist_, edge_vlist, edge_type); - bool success = db_.edges_.access().insert(edge_vlist).second; - const auto edge_accessor = EdgeAccessor(*edge_vlist, *this); if (success) { // This has to be here because there is no additional method for setting // edge type. + const auto edge_accessor = EdgeAccessor(*edge_vlist, *this); UpdateEdgeTypeIndex(edge_type, edge_accessor, &edge_accessor.current()); return edge_accessor; } @@ -336,10 +339,9 @@ int64_t GraphDbAccessor::Counter(const std::string &name) { } void GraphDbAccessor::CounterSet(const std::string &name, int64_t value) { - auto name_counter_pair = db_.counters_.access() - .emplace(name, std::make_tuple(name), std::make_tuple(value)); - if (!name_counter_pair.second) - name_counter_pair.first->second.store(value); + auto name_counter_pair = db_.counters_.access().emplace( + name, std::make_tuple(name), std::make_tuple(value)); + if (!name_counter_pair.second) name_counter_pair.first->second.store(value); } std::vector GraphDbAccessor::IndexInfo() const {