3
0
Fork 0
mirror of https://github.com/Z3Prover/z3 synced 2025-05-06 15:25:46 +00:00

garbage collect all api::object references when calling del_context. Request issue #679

Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>
This commit is contained in:
Nikolaj Bjorner 2016-07-13 22:26:21 -07:00
parent f30fb7639e
commit b080e3a216
25 changed files with 115 additions and 56 deletions

View file

@ -32,6 +32,28 @@ void install_tactics(tactic_manager & ctx);
namespace api {
object::object(context& c): m_ref_count(0), m_context(c) { this->m_id = m_context.add_object(this); }
void object::inc_ref() { m_ref_count++; }
void object::dec_ref() { SASSERT(m_ref_count > 0); m_ref_count--; if (m_ref_count == 0) m_context.del_object(this); }
unsigned context::add_object(api::object* o) {
unsigned id = m_allocated_objects.size();
if (!m_free_object_ids.empty()) {
id = m_free_object_ids.back();
m_free_object_ids.pop_back();
}
m_allocated_objects.insert(id, o);
return id;
}
void context::del_object(api::object* o) {
m_free_object_ids.push_back(o->id());
m_allocated_objects.remove(o->id());
dealloc(o);
}
static void default_error_handler(Z3_context ctx, Z3_error_code c) {
printf("Error: %s\n", Z3_get_error_msg(ctx, c));
exit(1);
@ -106,6 +128,14 @@ namespace api {
context::~context() {
reset_parser();
m_last_obj = 0;
u_map<api::object*>::iterator it = m_allocated_objects.begin();
while (it != m_allocated_objects.end()) {
//warning_msg("Junk: %d", it->m_key);
m_allocated_objects.remove(it->m_key);
dealloc(it->m_value);
it = m_allocated_objects.begin();
}
}
void context::interrupt() {