From 3068fbda8fa3946aba1d7df52a2c905d8e1ee87d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= Date: Thu, 23 Sep 2010 12:13:30 +0200 Subject: [PATCH] Preprocessor: no longer crashes when some dynamic variables appear only in unused model local variables (closes #101) --- preprocessor/ModelTree.cc | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/preprocessor/ModelTree.cc b/preprocessor/ModelTree.cc index ede21b364..dbfc62e9f 100644 --- a/preprocessor/ModelTree.cc +++ b/preprocessor/ModelTree.cc @@ -1143,11 +1143,18 @@ ModelTree::compileTemporaryTerms(ostream &code_file, unsigned int &instruction_n void ModelTree::writeModelLocalVariables(ostream &output, ExprNodeOutputType output_type) const { - for (map::const_iterator it = local_variables_table.begin(); - it != local_variables_table.end(); it++) + /* Collect all model local variables appearing in equations, and print only + them. Printing unused model local variables can lead to a crash (see + ticket #101). */ + set used_local_vars; + for (size_t i = 0; i < equations.size(); i++) + equations[i]->collectModelLocalVariables(used_local_vars); + + for (set::const_iterator it = used_local_vars.begin(); + it != used_local_vars.end(); ++it) { - int id = it->first; - expr_t value = it->second; + int id = *it; + expr_t value = local_variables_table.find(id)->second; if (IS_C(output_type)) output << "double ";