Skip to content

Commit 727770c

Browse files
committed
temporary hack for #579
1 parent fc629f1 commit 727770c

1 file changed

Lines changed: 56 additions & 2 deletions

File tree

core/species.cpp

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5671,8 +5671,62 @@ void Species::RetractNewIndividual()
56715671
// around the code since it seems to keep coming back...
56725672
//current_new_individual_ = nullptr;
56735673

5674-
for (TreeSeqInfo &tsinfo : treeseq_)
5675-
tsk_table_collection_truncate(&tsinfo.tables_, &tsinfo.table_position_);
5674+
size_t trees_count = treeseq_.size();
5675+
5676+
if (trees_count > 0)
5677+
{
5678+
TreeSeqInfo &tsinfo_0 = treeseq_[0];
5679+
5680+
// BCH 12/1/2025: The base table collection can restore its bookmarked position directly;
5681+
// that will reset the bookmarked positions in all of the shared tables as well.
5682+
tsk_table_collection_truncate(&tsinfo_0.tables_, &tsinfo_0.table_position_);
5683+
5684+
// BCH 12/1/2025: In the multichrom case we need to protect against a segfault inside
5685+
// tsk_table_collection_truncate() for the secondary table collections. This is because
5686+
// they have NULL for their various column pointers, and tsk_table_collection_truncate()
5687+
// accesses index 0 of every offset column to get the offset for row 0. (It is always
5688+
// for row 0 in the shared tables because they are zeroed out; their num_rows was zero
5689+
// in RecordTablePosition().) See https://github.com/MesserLab/SLiM/issues/579 for details.
5690+
// BEWARE: This code will need updating if new shared tables are added, or new columns
5691+
// are added within the existing shared table. Any offset column that is accessed in the
5692+
// ..._truncate() functions for the shared tables needs to be protected here.
5693+
tsk_size_t zero_value = 0;
5694+
tsk_size_t *pointer_to_zero_value = &zero_value;
5695+
5696+
for (size_t trees_index = 1; trees_index < trees_count; ++trees_index)
5697+
{
5698+
TreeSeqInfo &tsinfo_i = treeseq_[trees_index];
5699+
5700+
#if DEBUG
5701+
// This protection scheme relies upon the bookmarked row being zero for shared tables;
5702+
// only the zeroth element of each offset column is set up by the hack here.
5703+
if ((tsinfo_i.table_position_.nodes != 0) ||
5704+
(tsinfo_i.table_position_.individuals != 0) ||
5705+
(tsinfo_i.table_position_.populations != 0))
5706+
EIDOS_TERMINATION << "ERROR (Species::RetractNewIndividual): (internal error) tree sequence bookmark for a shared table in a secondary table collection is non-zero." << EidosTerminate();
5707+
#endif
5708+
5709+
tsinfo_i.tables_.nodes.metadata_offset = pointer_to_zero_value;
5710+
tsinfo_i.tables_.individuals.location_offset = pointer_to_zero_value;
5711+
tsinfo_i.tables_.individuals.parents_offset = pointer_to_zero_value;
5712+
tsinfo_i.tables_.individuals.metadata_offset = pointer_to_zero_value;
5713+
tsinfo_i.tables_.populations.metadata_offset = pointer_to_zero_value;
5714+
5715+
tsk_table_collection_truncate(&tsinfo_i.tables_, &tsinfo_i.table_position_);
5716+
5717+
tsinfo_i.tables_.nodes.metadata_offset = NULL;
5718+
tsinfo_i.tables_.individuals.location_offset = NULL;
5719+
tsinfo_i.tables_.individuals.parents_offset = NULL;
5720+
tsinfo_i.tables_.individuals.metadata_offset = NULL;
5721+
tsinfo_i.tables_.populations.metadata_offset = NULL;
5722+
}
5723+
}
5724+
5725+
// The above code boils down to this, which was the old code before #579 came along.
5726+
// It is the same apart from the complicated protection against segfaults:
5727+
//
5728+
// for (TreeSeqInfo &tsinfo : treeseq_)
5729+
// tsk_table_collection_truncate(&tsinfo.tables_, &tsinfo.table_position_);
56765730
}
56775731

56785732
void Species::RecordNewHaplosome(slim_position_t *p_breakpoints, int p_breakpoints_count, Haplosome *p_new_haplosome,

0 commit comments

Comments
 (0)