Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove backlinks when clearing list or set of Mixed containing links #4785

Merged
merged 2 commits into from
Jun 28, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)

### Breaking changes
* None.
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();
}