Skip to content

Open Source alternative to Algolia + Pinecone and an Easier-to-Use alternative to ElasticSearch ⚡ 🔍 ✨ Fast, typo tolerant, in-memory fuzzy Search Engine for building delightful search experiences

Fix memory leaks: * Handle deletion of `filter_tree_root` in `sort_fields_guard_t`. * Handle `filter_tree_root` being updated in `Index::static_filter_query_eval`. * Handle deletion of `phrase_result_ids` in `Index::search`.

Harpreet Sangarb152c88e8239
diff --git a/include/field.h b/include/field.h
index 8749af61..3acc254c 100644
--- a/include/field.h
+++ b/include/field.h
@@ -545,7 +545,7 @@ struct sort_by {
};
struct eval_t {
- filter_node_t* filter_tree_root;
+ filter_node_t* filter_tree_root = nullptr;
uint32_t* ids = nullptr;
uint32_t size = 0;
};
diff --git a/include/index.h b/include/index.h
index ce501fd5..c47f2289 100644
--- a/include/index.h
+++ b/include/index.h
@@ -643,7 +643,7 @@ public:
Option<bool> search(std::vector<query_tokens_t>& field_query_tokens, const std::vector<search_field_t>& the_fields,
const text_match_type_t match_type,
- filter_node_t* filter_tree_root, std::vector<facet>& facets, facet_query_t& facet_query,
+ filter_node_t*& filter_tree_root, std::vector<facet>& facets, facet_query_t& facet_query,
const int max_facet_values,
const std::vector<std::pair<uint32_t, uint32_t>>& included_ids,
const std::vector<uint32_t>& excluded_ids, std::vector<sort_by>& sort_fields_std,
diff --git a/src/collection.cpp b/src/collection.cpp
index b7197ce4..e7641abc 100644
--- a/src/collection.cpp
+++ b/src/collection.cpp
@@ -27,6 +27,8 @@ struct sort_fields_guard_t {
~sort_fields_guard_t() {
for(auto& sort_by_clause: sort_fields_std) {
+ delete sort_by_clause.eval.filter_tree_root;
+
if(sort_by_clause.eval.ids) {
delete [] sort_by_clause.eval.ids;
sort_by_clause.eval.ids = nullptr;
@@ -1524,6 +1526,10 @@ Option<nlohmann::json> Collection::search(std::string raw_query,
auto search_op = index->run_search(search_params, name, use_facet_intersection);
+ // filter_tree_root might be updated in Index::static_filter_query_eval.
+ filter_tree_root_guard.release();
+ filter_tree_root_guard.reset(filter_tree_root);
+
if (!search_op.ok()) {
return Option<nlohmann::json>(search_op.code(), search_op.error());
}
diff --git a/src/index.cpp b/src/index.cpp
index f36c5398..57ff7400 100644
--- a/src/index.cpp
+++ b/src/index.cpp
@@ -2453,7 +2453,7 @@ bool Index::static_filter_query_eval(const override_t* override,
if (filter_tree_root == nullptr) {
filter_tree_root = new_filter_tree_root;
} else {
- filter_node_t* root = new filter_node_t(AND, filter_tree_root,
+ auto root = new filter_node_t(AND, filter_tree_root,
new_filter_tree_root);
filter_tree_root = root;
}
@@ -2813,7 +2813,7 @@ void Index::search_infix(const std::string& query, const std::string& field_name
Option<bool> Index::search(std::vector<query_tokens_t>& field_query_tokens, const std::vector<search_field_t>& the_fields,
const text_match_type_t match_type,
- filter_node_t* filter_tree_root, std::vector<facet>& facets, facet_query_t& facet_query,
+ filter_node_t*& filter_tree_root, std::vector<facet>& facets, facet_query_t& facet_query,
const int max_facet_values,
const std::vector<std::pair<uint32_t, uint32_t>>& included_ids,
const std::vector<uint32_t>& excluded_ids, std::vector<sort_by>& sort_fields_std,
@@ -2900,6 +2900,8 @@ Option<bool> Index::search(std::vector<query_tokens_t>& field_query_tokens, cons
// handle phrase searches
uint32_t* phrase_result_ids = nullptr;
uint32_t phrase_result_count = 0;
+ std::unique_ptr<uint32_t> phrase_result_ids_guard;
+
if (!field_query_tokens[0].q_phrases.empty()) {
do_phrase_search(num_search_fields, the_fields, field_query_tokens,
sort_fields_std, searched_queries, group_limit, group_by_fields,
@@ -2908,6 +2910,9 @@ Option<bool> Index::search(std::vector<query_tokens_t>& field_query_tokens, cons
excluded_result_ids, excluded_result_ids_size, excluded_group_ids, curated_topster,
included_ids_map, is_wildcard_query,
phrase_result_ids, phrase_result_count);
+
+ phrase_result_ids_guard.reset(phrase_result_ids);
+
if (phrase_result_count == 0) {
goto process_search_results;
}