home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / lang / cplus / 18835 < prev    next >
Encoding:
Text File  |  1993-01-08  |  2.0 KB  |  70 lines

  1. Path: sparky!uunet!timbuk.cray.com!equalizer!sdcrsi!xlnt!ventura!vs@UCSD.EDU
  2. From: ventura!vs@UCSD.EDU (Shankar Venkataraman)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Need Help on Debugging Objects
  5. Message-ID: <257@ventura.UUCP>
  6. Date: 8 Jan 93 01:21:07 GMT
  7. References: <256@ventura.UUCP>
  8. Sender: news@ventura.UUCP
  9. Reply-To: ventura!vs@UCSD.EDU
  10. Organization: Ventura Software, Inc., A XEROX Company
  11. Lines: 56
  12. Nntp-Posting-Host: vpdsb
  13.  
  14. In article 256@ventura.UUCP, ventura!tim@UCSD.EDU (Tim Dierks) writes:
  15. >I am looking for ideas on how to track the creation/deletion of objects for
  16. >debugging purposes in an application I am writing.  For the PC, The Microsoft
  17. >Foundation Class offers a TRACE macro that lets you display a message, but I'd like
  18. >more functionality, and I don't plan on use the MFC.  I'd like to be able to do
  19. >things like those listed below:
  20. >
  21. >- Display a message each time an object is created/deleted, identifying what type of
  22. >  object it is, and some unique identifier for the object.
  23. >- Identify the creator of each object when created.
  24. >- Be able to dump the data members of any object currently in existence.
  25. >- Be able to list all the objects currently in existence at any time.
  26. >
  27. >Has anyone had any experience with this?  Are there already existing class
  28. >libraries or tools that do this sort of thing?  I'd appreciate any ideas.
  29. >
  30. One way to achieve this is by defining a class with a static member and declaring an
  31. instance of the class in every class you want to debug. The constructor and destructor 
  32. of the "trace" class give messages during creation and deletion.
  33.  
  34. class Trace {
  35. private :
  36.     static int ObjectCount;
  37.     int id;
  38. public :
  39.     Trace : id(++ObjectCount)
  40.     {
  41.         cout <<  "Constructing Object # "<< id << "\n";
  42.     }
  43.     ~Trace {
  44.         cout << "Deleting Object # " << id << "\n";
  45.     }
  46. };
  47.  
  48. class MyClass {
  49. private :
  50.  
  51.     int MyData;
  52.  
  53. #ifdef DEBUG_CLASS
  54.     Trace MyTrace;
  55. #endif
  56.  
  57. public :
  58.     MyClass{};
  59.     ...
  60.     ...
  61. };
  62.  
  63. This should help you to debug creation and deletion of objects.
  64.  
  65. Shankar V
  66. ventura!vs@ucsd.edu
  67.  
  68.  
  69.         
  70.