[ create a new paste ] login | about

Link: http://codepad.org/RISHn15X    [ raw code | output | fork ]

C++, pasted on Mar 15:
class BaseBitmap {

    BaseBitmap() {
    }

    ~BaseBitmap() {
    }

public:

    static int allocs;
    static int deallocs;

    static BaseBitmap* Alloc() {
        allocs++;
        return new BaseBitmap;
    }

    static void Free(BaseBitmap*& ptr) {
        if (ptr) {
            deallocs++;
            delete ptr;
            ptr = NULL;
        }
    }

};

int BaseBitmap::allocs = 0;
int BaseBitmap::deallocs = 0;

class MyDialog {

    std::vector<BaseBitmap*> bmps;

public:

    ~MyDialog() {
        std::vector<BaseBitmap*>::iterator it = bmps.begin();
        for (; it != bmps.end(); it++) {
            BaseBitmap::Free(*it);
        }
        bmps.clear();
    }

    int Message(void* node, int msgType, void* pData) {
        if (msgType == 100) {
            BaseBitmap* bmp = BaseBitmap::Alloc();
            if (bmp) {
                std::cout << "Adding bitmap..\n";
                bmps.push_back(bmp);
            }
        }
        return 1;
    }

};

int main() {
    /* scope */ {
        MyDialog dlg;
        dlg.Message(NULL, 20, NULL);
        dlg.Message(NULL, 100, NULL);
        dlg.Message(NULL, 100, NULL);
    }

    std::cout << "BaseBitmap Allocs: " << BaseBitmap::allocs << "\n";
    std::cout << "BaseBitmap Deallocs: " << BaseBitmap::deallocs << "\n";
}


Output:
1
2
3
4
Adding bitmap..
Adding bitmap..
BaseBitmap Allocs: 2
BaseBitmap Deallocs: 2


Create a new paste based on this one


Comments: