5 #include "SharedPtr.hpp"
6 #include "../ast_fwd_decl.hpp"
8 #ifdef DEBUG_SHARED_PTR
9 #include "../debugger.hpp"
14 #ifdef DEBUG_SHARED_PTR
15 void SharedObj::dumpMemLeaks() {
17 std::cerr << "###################################\n";
18 std::cerr << "# REPORTING MISSING DEALLOCATIONS #\n";
19 std::cerr << "###################################\n";
20 for (auto var : all) {
21 if (AST_Node_Ptr ast = Cast<AST_Node>(var)) {
24 std::cerr << "LEAKED " << var << "\n";
29 std::vector<SharedObj*> SharedObj::all;
32 bool SharedObj::taint = false;
34 SharedObj::SharedObj()
36 #ifdef DEBUG_SHARED_PTR
41 #ifdef DEBUG_SHARED_PTR
42 if (taint) all.push_back(this);
46 SharedObj::~SharedObj() {
47 #ifdef DEBUG_SHARED_PTR
48 if (dbg) std::cerr << "Destruct " << this << "\n";
49 if(!all.empty()) { // check needed for MSVC (no clue why?)
50 all.erase(std::remove(all.begin(), all.end(), this), all.end());
57 void SharedPtr::decRefCount() {
60 #ifdef DEBUG_SHARED_PTR
61 if (node->dbg) std::cerr << "- " << node << " X " << node->refcounter << " (" << this << ") " << "\n";
63 if (node->refcounter == 0) {
64 #ifdef DEBUG_SHARED_PTR
65 AST_Node_Ptr ptr = Cast<AST_Node>(node);
66 if (node->dbg) std::cerr << "DELETE NODE " << node << "\n";
68 if (!node->detached) {
75 void SharedPtr::incRefCount() {
78 node->detached = false;
79 #ifdef DEBUG_SHARED_PTR
81 std::cerr << "+ " << node << " X " << node->refcounter << " (" << this << ") " << "\n";
87 SharedPtr::~SharedPtr() {
92 // the create constructor
93 SharedPtr::SharedPtr(SharedObj* ptr)
97 // copy assignment operator
98 SharedPtr& SharedPtr::operator=(const SharedPtr& rhs) {
99 void* cur_ptr = (void*) node;
100 void* rhs_ptr = (void*) rhs.node;
101 if (cur_ptr == rhs_ptr) {
110 // the copy constructor
111 SharedPtr::SharedPtr(const SharedPtr& obj)