At this point I've traced it a bit further and believe the static `ScalarSparsity` in `Sparsity::getScalar()` is somehow getting corrupted. As far as I understand, the row index passed into the `operator()` call is getting implicitly converted into a `Matrix<casadi_int>` via the `Matrix(double val)` constructor when `self().get(ret, false, rr)` is called from `GenericMatrix`, and somehow the sparsity object corresponding to the converted `const Matrix<casadi_int>& rr` object is getting set to the sparsity object for the original `matrix` object (`sp_.size()` is identical, and so is the memory address returned by `colind()`). When I see the correct non-crashing behavior, the sparsity pattern for the row index matrix has the correct `sp_.size() == 5` and a static memory address for `colind()`. I'm checking this with some prints in `SparsityInternal`, where I see the attached output during an `operator()` function call directly before the crash. Anyway, I'm going to keep digging and will try to figure out more, but let me know if any of this starts ringing bells.
/// Number of structural non-zeros
inline casadi_int nnz() const {
std::cout << "In nnz()" << std::endl;
std::cout << "size2(): " << size2() << std::endl;
std::cout << "sp_.size(): " << sp_.size() << std::endl;
std::cout << "colind(): " << colind() << std::endl;
return colind()[size2()];
}
----------------------------------------------
Constructing symbolic matrix with name: state_0
70098724 Creating new sparsity pattern (not using cache)
70098725 In nnz()
70098726 size2(): 1
70098727 sp_.size(): 9
70098728 colind(): 0x244f6c0
70098729 In nnz()
70098730 size2(): 1
70098731 sp_.size(): 9
70098732 colind(): 0x244f6c0
70098733 In nnz()
70098734 size2(): 1
70098735 sp_.size(): 9
70098736 colind(): 0x244f6c0
70098737 In nnz()
70098738 size2(): 1
70098739 sp_.size(): 9
70098740 colind(): 0x244f6c0
70098741 In nnz()
70098742 size2(): 1
70098743 sp_.size(): 9
70098744 colind(): 0x244f6c0
70098745 In nnz()
70098746 size2(): 1
70098747 sp_.size(): 9
70098748 colind(): 0x244f6c0
70098749 In nnz()
70098750 size2(): 1
70098751 sp_.size(): 9
70098752 colind(): 0x244f6c0
70098753 Symbolic vector row size: 5
70098754 Symbolic vector column size: 1
-------------------------------------------------
Accessing matrix element (0):
70098756 In GenericMatrix::operator()
In void Matrix<Scalar>::get(Matrix<Scalar>& m, bool ind1, const Matrix<casadi_int>& rr)
70098758 In nnz()
70098759 size2(): 1
70098760 sp_.size(): 9
70098761 colind(): 0x244f6c0
70098762 In nnz()
70098763 size2(): 1
70098764 sp_.size(): 9
70098765 colind(): 0x244f6c0