safaei@brahma.trl.oz.au (Farzad Safaei) (08/24/90)
I have been trying to keep track of my program's memory handling by overloading "new" and "delete". To do this, I defined "ObjectCounter" class as the base class of all the other objects which were likely to be dynamically created. Although this seems to work under Unix (using AT&T c++ compiler), I have not been able to run it under OS/2 (with Zortech compiler). The problem, so far as I could work out, may be due to non-static member variables and/or virtual member functions in the derived classes. In any case it causes a crash after few calls to new and/or delete. I include a short test programs. As it stands it does not work and crashes pretty soon. If, however, I change the variable "someVar" in class "Intermediate" to static, or remove the virtual destructor everything will be fine . Any suggestions? Is there any other way to get the amount of memory used by the program under OS/2? --------------begin including test program----------------------- #include <stddef.h> #include <stdio.h> class ObjectCounter { static int objCount; public: ObjectCounter() {} static void initObjLog() {objCount=0;} void* operator new(size_t ) { objCount++; void* p=::new(size_t); fprintf(stderr,"New object created: count= %d pointer= %d\n",objCount,(int)p); return(p); } void operator delete(void* dp) { objCount--; fprintf(stderr,"Object deleted: count= %d pointer= %d\n",objCount,(int)dp); ::delete(dp); } }; class Intermediate:public ObjectCounter { int someVar; public: Intermediate() {someVar = 1;} virtual ~Intermediate() {} }; class dummy: public Intermediate { public: dummy() {} }; class someObj { public: someObj() {} void create() { int i; dummy* p2d[10]; for (i=0;i<10;i++) p2d[i] = new dummy(); for (i=0;i<10;i++) delete p2d[i]; } }; int main(void); main() { ObjectCounter::initObjLog(); someObj obj; obj.create(); } ------------------------end test program-------------------------