Fixed memory bug in write_smt2
authorClifford Wolf <clifford@clifford.at>
Tue, 30 Aug 2016 12:49:47 +0000 (14:49 +0200)
committerClifford Wolf <clifford@clifford.at>
Tue, 30 Aug 2016 12:49:47 +0000 (14:49 +0200)
backends/smt2/smt2.cc

index 1885cf59a348b92960032162a7c232a8afb259fa..9668a64256d3e3df13d04df37ab1bcd3c49f44c7 100644 (file)
@@ -43,7 +43,25 @@ struct Smt2Worker
        std::map<RTLIL::SigBit, std::pair<int, int>> fcache;
        std::map<Cell*, int> memarrays;
        std::map<int, int> bvsizes;
-       std::vector<string> ids;
+       dict<IdString, char*> ids;
+
+       const char *get_id(IdString n)
+       {
+               if (ids.count(n) == 0) {
+                       std::string str = log_id(n);
+                       for (int i = 0; i < GetSize(str); i++) {
+                               if (str[i] == '\\')
+                                       str[i] = '/';
+                       }
+                       ids[n] = strdup(str.c_str());
+               }
+               return ids[n];
+       }
+
+       template<typename T>
+       const char *get_id(T *obj) {
+               return get_id(obj->name);
+       }
 
        Smt2Worker(RTLIL::Module *module, bool bvmode, bool memmode, bool wiresmode, bool verbose) :
                        ct(module->design), sigmap(module), module(module), bvmode(bvmode), memmode(memmode),
@@ -68,15 +86,11 @@ struct Smt2Worker
                }
        }
 
-       const char *get_id(IdString n)
+       ~Smt2Worker()
        {
-               std::string str = log_id(n);
-               for (int i = 0; i < GetSize(str); i++) {
-                       if (str[i] == '\\')
-                               str[i] = '/';
-               }
-               ids.push_back(str);
-               return ids.back().c_str();
+               for (auto &it : ids)
+                       free(it.second);
+               ids.clear();
        }
 
        const char *get_id(Module *m)