Skip to content

Commit

Permalink
Merge pull request #4785 from realm/je/fix-mixed-clear
Browse files Browse the repository at this point in the history
Remove backlinks when clearing list or set of Mixed containing links
  • Loading branch information
jedelbo committed Jun 28, 2021
2 parents aa3251b + 75aceec commit e5c8b29
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* Fixed an assertion failure when listening for changes to a list of primitive Mixed which contains links. ([#4767](https://github.com/realm/realm-core/issues/4767), since the beginning of Mixed v11.0.0)
* Fixed an assertion failure when listening for changes to a dictionary or set which contains an invalidated link. ([#4770](https://github.com/realm/realm-core/pull/4770), since the beginning of v11)
* Fixed an endless recursive loop that could cause a stack overflow when computing changes on a set of objects which contained cycles. ([#4770](https://github.com/realm/realm-core/pull/4770), since the beginning of v11)
* Fixed a crash after clearing a list or set of Mixed containing links to objects ([#4774](https://github.com/realm/realm-core/issues/4774), since the beginning of v11)
* Fixed a recursive loop which would eventually crash trying to refresh a user app token when it had been revoked by an admin. Now this situation logs the user out and reports an error. ([#4745](https://github.com/realm/realm-core/issues/4745), since v10.0.0).
* Fixed a race between calling realm::delete_files and concurent opening of the realm file.([#4768](https://github.com/realm/realm-core/pull/4768))

Expand Down
9 changes: 9 additions & 0 deletions src/realm/list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,15 @@ void Lst<Mixed>::do_remove(size_t ndx)
}
}

template <>
void Lst<Mixed>::do_clear()
{
size_t ndx = size();
while (ndx--) {
do_remove(ndx);
}
}

Obj LnkLst::create_and_insert_linked_object(size_t ndx)
{
Table& t = *get_target_table();
Expand Down
2 changes: 2 additions & 0 deletions src/realm/list.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,8 @@ template <>
void Lst<Mixed>::do_insert(size_t, Mixed);
template <>
void Lst<Mixed>::do_remove(size_t);
template <>
void Lst<Mixed>::do_clear();
extern template class Lst<Mixed>;

// Specialization of Lst<ObjLink>:
Expand Down
9 changes: 9 additions & 0 deletions src/realm/set.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,15 @@ void Set<Mixed>::do_erase(size_t ndx)
}
}

template <>
void Set<Mixed>::do_clear()
{
size_t ndx = size();
while (ndx--) {
do_erase(ndx);
}
}

void LnkSet::remove_target_row(size_t link_ndx)
{
// Deleting the object will automatically remove all links
Expand Down
2 changes: 2 additions & 0 deletions src/realm/set.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,8 @@ template <>
void Set<Mixed>::do_insert(size_t, Mixed);
template <>
void Set<Mixed>::do_erase(size_t);
template <>
void Set<Mixed>::do_clear();

/// Compare set elements.
///
Expand Down
25 changes: 25 additions & 0 deletions test/test_typed_links.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,3 +198,28 @@ TEST(TypedLinks_Clear)
person->clear();
g.verify();
}

TEST(TypedLinks_CollectionClear)
{
Group g;
auto dog = g.add_table("dog");
auto person = g.add_table("person");
auto col_list_mixed = person->add_column_list(type_Mixed, "mixed_list");
auto col_set_mixed = person->add_column_set(type_Mixed, "mixed_set");

auto pluto = dog->create_object();
auto paul = person->create_object();

auto list = paul.get_list<Mixed>(col_list_mixed);
auto set = paul.get_set<Mixed>(col_set_mixed);
list.add(pluto);
set.insert(pluto);
CHECK_EQUAL(pluto.get_backlink_count(), 2);
list.clear();
set.clear();
CHECK_EQUAL(pluto.get_backlink_count(), 0);

pluto.remove();

g.verify();
}

0 comments on commit e5c8b29

Please sign in to comment.