Fix MutSel potential bugs related to alignment copying - #211
Conversation
…by alignment-copying functions
bqminh
left a comment
There was a problem hiding this comment.
mostly good thank you. I'll merge this now and update one thing to remote num_rates from Alignment
|
Do you think it would be better to change it from a pointer to either a I feel otherwise this will inevitably (or already does) lead to memory leaks and use after free. |
|
Your suggestion for using vectors and smart pointers is reasonable from the standpoint of the modern C++ standards, and I would definitely opt to follow these standards myself if I were to write a new program. But here we are discussing a small addition to the IQ-Tree code, in which there are C-style arrays everywhere and hardly any smart pointers at all. So in order to be consistent, we should either decide on rewriting the whole program or just opt to follow the existing style. It is worth noting that using C-style arrays is not evil by itself, but merely has to be done with a bit of caution. Why there is definitely no usage after delete now: Why there are no memory leaks and any such bugs are very unlikely to appear in the future: The occasional need to assign a single array pointer to multiple patterns, as it is done in the I suggest a compromise solution. We'll keep the types of both members as |
|
Agreed! Actually there are many things that I could have done differently if we were to write IQ-TREE now. The original code was designed 15 years ago, and over time I found things that could have been better designed. For example, the tree data structure can be better designed (e.g. merging Node and Neighbor). The Alignment structure can also be embedded better into the likelihood calculations (as Alignment sits at the tips of the tree), so that likelihood is computed more efficiently. In any case, one thing we need to keep is: to retain the full git history, so that contributions of all developers/contributors are clearly recognised. When I have time I will write down all the ideas I may have wanted to do differently. I thought it can never come to fruition, but now with the age of AI, it may become possible, actually. |
This PR simplifies and solidifies the logic of how pattern-specific rate matrices are handled. It introduces the following changes:
vector<double*> Alignment::ptn_rate_matvector as C-style arrays in the row-major order. This design makesptn_rate_matlargely similar in use toptn_state_freq.int Alignment::num_ratesmember to makeptn_rate_matcompatible with various data types and non-reversible models and possibly useful beyond MutSel.ptn_rate_matare copied along withptn_state_freqby the alignment-copying functions (extractSubAlignment(),extractPatterns(),extractSites(), etc.). IMPORTANT: This fixes potential bugs of MutSel related to those I previously fixed for PMSF.mutsel_wrapper.cpp: rate matrix variables use names and data types more similar to those of frequency vector variables; thewrite_site_models_to_alignment()function prints at the end the total number of the pattern-specific matrix-vector pairs instead of site-specific such pairs, which is now similar to theAlignment::readSiteStateFreq()function's printed log.