Fix conversions from Teuchos::Tuple to Teuchos::ArrayView that can detect dangling references
Created by: bartlettroscoe
CC: @trilinos/teuchos
Description:
Currently, in a debug-mode build, the Teuchos Memory Management classes can not detect and report dangling references of Teuchos::ArrayView on Teuchos::Tuple objects like:
Teuchos::ArrayView<int> av = tuple<int, 2>(1,2);
std::cout << av(0);
std::cout << av(1);
The current implementation of Teuchos::Tuple is just a sublcass of Teuchos::ArrayView and code does not pick up that av
is a view into a now deleted Tuple<int,2>
object. An example of this a failure that resulted due to this in #1326 (closed). This is an error that valgrind can not detect because it involves stack-based memory. Therefore, this results in undefined behavior that is very hard to detect with automated tools like that.
Fixing this might be quite hard due to backward compatibility constraints.
One option is to refactor Teuchos::Tuple to not derive from ArrayView and add appropriate unit tests. That would likely have the largest non-backward compatible impact because it would break implicit conversion of Teuchos:Tuple to Teuchos::ArrayView, even in template function calls (due to the magic of derivation of types in C++).
Another option is to keep Teuchos::Tuple deriving from Teuchos::ArrayView but to add explicit copy constructors and assignment operators for Teuchos::ArrayView that take Teuchos::Tuple and correctly pick up the dangling reference. It is not clear if this can be made to work but it would likely have the least (if any) non-backward compatible impact if we can make it work. That is because implicit conversions of references of Teuchos::Tuple to Teuchos::ArrayView would still be maintained (even magical conversions in templated code). This would only impact backward compatibility in rare largely invalid usage cases.