Skip to content

Commit ca7040f

Browse files
authored
Merge pull request #2876 from martinhsv/v3/master
Resolve memory leak (bison-generated position.filename)
2 parents 5f632a5 + 55d6aa9 commit ca7040f

File tree

7 files changed

+953
-943
lines changed

7 files changed

+953
-943
lines changed

src/parser/driver.cc

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Driver::Driver()
3434

3535

3636
Driver::~Driver() {
37+
3738
while (loc.empty() == false) {
3839
yy::location *a = loc.back();
3940
loc.pop_back();
@@ -129,9 +130,11 @@ int Driver::parse(const std::string &f, const std::string &ref) {
129130
m_lastRule = nullptr;
130131
loc.push_back(new yy::location());
131132
if (ref.empty()) {
132-
loc.back()->begin.filename = loc.back()->end.filename = new std::string("<<reference missing or not informed>>");
133+
m_filenames.push_back("<<reference missing or not informed>>");
134+
loc.back()->begin.filename = loc.back()->end.filename = &(m_filenames.back());
133135
} else {
134-
loc.back()->begin.filename = loc.back()->end.filename = new std::string(ref);
136+
m_filenames.push_back(ref);
137+
loc.back()->begin.filename = loc.back()->end.filename = &(m_filenames.back());
135138
}
136139

137140
if (f.empty()) {

src/parser/driver.h

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,6 @@ typedef struct Driver_t Driver;
5353
#endif
5454

5555

56-
/**
57-
*
58-
* FIXME: There is a memory leak in the filename at yy::location.
59-
* The filename should be converted into a shared string to
60-
* save memory or be associated with the life cycle of the
61-
* driver class.
62-
*
63-
**/
6456
class Driver : public RulesSetProperties {
6557
public:
6658
Driver();
@@ -92,6 +84,13 @@ class Driver : public RulesSetProperties {
9284
RuleWithActions *m_lastRule;
9385

9486
RulesSetPhases m_rulesSetPhases;
87+
88+
// Retain a list of new'd filenames so that they are available during the lifetime
89+
// of the Driver object, but so that they will get cleaned up by the Driver
90+
// destructor. This is to resolve a memory leak of yy.position.filename in location.hh.
91+
// Ordinarily other solutions would have been preferable, but location.hh is a
92+
// bison-generated file, which makes some alternative solutions impractical.
93+
std::list<std::string> m_filenames;
9594
};
9695

9796

0 commit comments

Comments
 (0)