Skip to content

Commit 2543943

Browse files
committed
Manage lifetime of MiniSat solver pointer using unique_ptr
This has the benefit of allowing the use of a default destructor rather than a hand written destructor. Note that `satcheck_minisat2_baset` was already non-copyable because its `solver_hardness` field was non-copyable. The default implementation of the destructor is in the `.cpp` rather than the header file in order to avoid recompiling it every time the header file is included.
1 parent e7b6d76 commit 2543943

File tree

3 files changed

+13
-23
lines changed

3 files changed

+13
-23
lines changed

scripts/expected_doxygen_warnings.txt

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,6 @@
66
template
77
satcheck_glucose_baset< Glucose::SimpSolver >::~satcheck_glucose_baset()
88

9-
/home/runner/work/cbmc/cbmc/src/solvers/sat/satcheck_minisat2.cpp:313: warning: no matching class member found for
10-
template
11-
satcheck_minisat2_baset< Minisat::Solver >::~satcheck_minisat2_baset()
12-
13-
/home/runner/work/cbmc/cbmc/src/solvers/sat/satcheck_minisat2.cpp:319: warning: no matching class member found for
14-
template
15-
satcheck_minisat2_baset< Minisat::SimpSolver >::~satcheck_minisat2_baset()
16-
179
warning: Include graph for 'goto_instrument_parse_options.cpp' not generated, too many nodes (97), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.
1810
warning: Included by graph for 'goto_functions.h' not generated, too many nodes (66), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.
1911
warning: Included by graph for 'goto_model.h' not generated, too many nodes (111), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.

src/solvers/sat/satcheck_minisat2.cpp

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Author: Daniel Kroening, [email protected]
1818
#include <stack>
1919

2020
#include <util/invariant.h>
21+
#include <util/make_unique.h>
2122
#include <util/threeval.h>
2223

2324
#include <minisat/core/Solver.h>
@@ -227,7 +228,7 @@ propt::resultt satcheck_minisat2_baset<T>::do_prop_solve()
227228

228229
if(time_limit_seconds != 0)
229230
{
230-
solver_to_interrupt = solver;
231+
solver_to_interrupt = solver.get();
231232
old_handler = signal(SIGALRM, interrupt_solver);
232233
if(old_handler == SIG_ERR)
233234
log.warning() << "Failed to set solver time limit" << messaget::eom;
@@ -241,7 +242,7 @@ propt::resultt satcheck_minisat2_baset<T>::do_prop_solve()
241242
{
242243
alarm(0);
243244
signal(SIGALRM, old_handler);
244-
solver_to_interrupt = solver;
245+
solver_to_interrupt = solver.get();
245246
}
246247

247248
#else // _WIN32
@@ -309,21 +310,14 @@ void satcheck_minisat2_baset<T>::set_assignment(literalt a, bool value)
309310
template <typename T>
310311
satcheck_minisat2_baset<T>::satcheck_minisat2_baset(
311312
message_handlert &message_handler)
312-
: cnf_solvert(message_handler), solver(new T), time_limit_seconds(0)
313+
: cnf_solvert(message_handler),
314+
solver(util_make_unique<T>()),
315+
time_limit_seconds(0)
313316
{
314317
}
315318

316-
template<>
317-
satcheck_minisat2_baset<Minisat::Solver>::~satcheck_minisat2_baset()
318-
{
319-
delete solver;
320-
}
321-
322-
template<>
323-
satcheck_minisat2_baset<Minisat::SimpSolver>::~satcheck_minisat2_baset()
324-
{
325-
delete solver;
326-
}
319+
template <typename T>
320+
satcheck_minisat2_baset<T>::~satcheck_minisat2_baset() = default;
327321

328322
template<typename T>
329323
bool satcheck_minisat2_baset<T>::is_in_conflict(literalt a) const

src/solvers/sat/satcheck_minisat2.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ Author: Daniel Kroening, [email protected]
1414

1515
#include <solvers/hardness_collector.h>
1616

17+
#include <memory>
18+
1719
// Select one: basic solver or with simplification.
1820
// Note that the solver with simplifier isn't really robust
1921
// when used incrementally, as variables may disappear
@@ -30,6 +32,8 @@ class satcheck_minisat2_baset : public cnf_solvert, public hardness_collectort
3032
{
3133
public:
3234
explicit satcheck_minisat2_baset(message_handlert &message_handler);
35+
/// A default destructor defined in the `.cpp` is used to ensure the
36+
/// unique_ptr to the solver is correctly destroyed.
3337
~satcheck_minisat2_baset() override;
3438

3539
tvt l_get(literalt a) const override final;
@@ -81,7 +85,7 @@ class satcheck_minisat2_baset : public cnf_solvert, public hardness_collectort
8185
protected:
8286
resultt do_prop_solve() override;
8387

84-
T *solver;
88+
std::unique_ptr<T> solver;
8589
uint32_t time_limit_seconds;
8690

8791
void add_variables();

0 commit comments

Comments
 (0)