Solved

dumping content of an object

Posted on 2002-05-13
25
985 Views
Last Modified: 2013-12-14
is there any better way of dumping content of an object on screen/debugger watch window in vc++ debugger.
in case of STL objects, the debugger watch window is really painful.

I'm not willing to use autoexp.dat file.
0
Comment
Question by:prashant_n_mhatre
  • 14
  • 6
  • 2
  • +2
25 Comments
 
LVL 49

Expert Comment

by:DanRollins
Comment Utility
>> really painful
I agree, and this is one of the prime reasons that I refuse to use STL.  The other is that tracing through the source code is excruciating.  Simple program errors that should take a few minutes to isolate and debug turn into all-night marathons.  

Contrast the std::string class to MFC's CString.  Both have similar features.  But with the latter, the code is clear and the data is at your fingertips.

About the only thing you can do is write a custom object-dumper.  It's output can go to the Debug window (TRACE) or to a log file.  The alternaive is avoid STL.

-- Dan
0
 
LVL 30

Expert Comment

by:Axter
Comment Utility
The problem is not with STL, but with the IDE.

VC++ IDE is clearly gear towards MFC, and makes little effort to interface properly with STL code.
0
 
LVL 2

Expert Comment

by:xLs
Comment Utility
i guess you are aware that watch window does intepretation as if it was runtime code.
so code casting and depth mem accessing can be made within the watch window..

such as a typical stl watch:

// depending on the template class

((CMyClass*) (*myiterator))->m_arrayinclass[mycounter];
or dumping complete class (without the stl jumbo)

(CMyClass*) *myiterator;

i agree STL "was" painful to debug.. but its all about getting used to and learn a good way to debug it.
0
 
LVL 30

Expert Comment

by:Axter
Comment Utility
Here's some code for putting some STL types to your TRACE window.

#pragma warning (disable:4786)
#include <sstream>
#include <string>

#ifdef _DEBUG
namespace DEBUG_CODE
{
     template<typename T>
     void TraceSTL_Obj(const char* SourceFileName, int SourceLineNum, const char* ObjectName, T t, int Index = -1)
     {
          std::ostringstream oss;
          oss << SourceFileName << "(" << SourceLineNum << ") : {" << ObjectName;
          if (Index > -1) oss << "[" << Index << "]";
          oss << "} = " << t << std::endl;
          TRACE(oss.str().c_str());
     }
     template<typename T>
     void TraceSTL_Container(const char* SourceFileName, int SourceLineNum, const char* ObjectName, T t)
     {
          int Idx = 0;
          for (T::const_iterator i = t.begin();i != t.end();++i, ++Idx)
          {
               std::string szObjectName = ObjectName;
               TraceSTL_Obj(SourceFileName, SourceLineNum, ObjectName, *i, Idx);
          }
     }
     template<typename T>
     void TraceSTL_AsscContainer(const char* SourceFileName, int SourceLineNum, const char* ObjectName, T t)
     {
          int Idx = 0;
          for (T::const_iterator i = t.begin();i != t.end();++i, ++Idx)
          {
               std::string szObjectName = ObjectName;
               TraceSTL_Obj(SourceFileName, SourceLineNum, ObjectName, i->first, Idx);
          }
     }
}
#define TRACE_STL_OBJ(t)              DEBUG_CODE::TraceSTL_Obj(__FILE__, __LINE__, #t, t)
#define TRACE_STL_CONTAINER(t)              DEBUG_CODE::TraceSTL_Container(__FILE__, __LINE__, #t, t)
#define TRACE_STL_ASSC_CONTAINER(t)              DEBUG_CODE::TraceSTL_AsscContainer(__FILE__, __LINE__, #t, t)
#else //_DEBUG
#define TRACE_STL_OBJ(t)            
#define TRACE_STL_CONTAINER(t)            
#define TRACE_STL_ASSC_CONTAINER(t)
#endif //_DEBUG
0
 
LVL 30

Expert Comment

by:Axter
Comment Utility
Example usage:
#pragma warning (disable:4786)
#include <sstream>
#include <string>
#include <list>
#include <vector>
#include <deque>
#include <map>

CTestTraceSTLApp::CTestTraceSTLApp()
{
     std::vector<std::string> MyStringVect;
     MyStringVect.push_back("Test1");
     MyStringVect.push_back("Test2");
     MyStringVect.push_back("Test3");
     MyStringVect.push_back("Test4");
     TRACE_STL_CONTAINER(MyStringVect);

     std::list<std::string> MyStringList;
     MyStringList.push_back("Testx1");
     MyStringList.push_back("Testx2");
     MyStringList.push_back("Testx3");
     TRACE_STL_CONTAINER(MyStringList);

     std::map<int, std::string> MyStringMap;
     MyStringMap[69] = "Test69";
     MyStringMap[77] = "Test77";
     MyStringMap[33] = "Test33";
     TRACE_STL_ASSC_CONTAINER(MyStringMap);

     std::string test1 = "Hello World! from std::string";
     TRACE_STL_OBJ(test1);

     CString test2 = "Hello World! from CString";
     TRACE_STL_OBJ(test2);
        AfxMessageBox("Check your debug window for above data");
}
0
 
LVL 6

Expert Comment

by:snoegler
Comment Utility
0
 
LVL 6

Expert Comment

by:snoegler
Comment Utility
Ooops sorry i didn't see the last line in your question :)
0
 
LVL 4

Author Comment

by:prashant_n_mhatre
Comment Utility
Thank you all.

I tried out a small test program

//----------------------------------
#pragma warning (disable:4786)
#include <string>
#include <list>
#include <iostream>
using namespace std;

typedef list<string> STRINGLIST;


void main()
{
     STRINGLIST  m_IssueList;
         
     m_IssueList.insert(m_IssueList.end(), "AAA");
     m_IssueList.insert(m_IssueList.end(), "BBB");
     m_IssueList.insert(m_IssueList.end(), "CCC");
         
     STRINGLIST::iterator Issues;
     
     for(Issues = m_IssueList.begin(); Issues != m_IssueList.end(); Issues++)
     {
          cerr << *Issues << endl;
     }
     
}
//-------------------------------------

What is the simplest way to see the content of (*Issues) and m_IssueList in debugger watch window?
0
 
LVL 30

Expert Comment

by:Axter
Comment Utility
Use the STL trace code I posted above.
TRACE_STL_ASSC_CONTAINER(m_IssueList);


TRACE_STL_OBJ(*Issues);
0
 
LVL 30

Expert Comment

by:Axter
Comment Utility
Correction:
TRACE_STL_CONTAINER(m_IssueList);


TRACE_STL_OBJ(*Issues);
0
 
LVL 30

Expert Comment

by:Axter
Comment Utility
Or you can modify your code to the following:
for(Issues = m_IssueList.begin(); Issues != m_IssueList.end(); Issues++)
    {
         //cerr << *Issues << endl;
         TRACE("%s",Issues->c_str());
    }
0
 
LVL 4

Author Comment

by:prashant_n_mhatre
Comment Utility
TRACE doesn't work in console application. Do I need to write my own TRACE macro?

I guess simple 'printf' will work.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 30

Expert Comment

by:Axter
Comment Utility
TRACE does work from a console application:
You need to include "afx.h"

Example:

#include "stdafx.h"

#include <stdlib.h>

#include <afx.h>

int main(int argc, char* argv[])
{
     TRACE("Hello from TRACE");

     system("pause");
     return 0;
}
0
 
LVL 30

Expert Comment

by:Axter
Comment Utility
You might need to modify your project settings under C++ TAB and [Code Generation], and change run-time library to Multithread.

Only do this if you get errors like the following:
nafxcwd.lib(thrdcore.obj) : error LNK2001: unresolved external symbol __endthreadex
nafxcwd.lib(thrdcore.obj) : error LNK2001: unresolved external symbol __beginthreadex
0
 
LVL 4

Author Comment

by:prashant_n_mhatre
Comment Utility
Do I need to create win32 application? I tried including <afx.h> in console application. It gives linking error like:

Linking...
nafxcwd.lib(afxmem.obj) : error LNK2005: "void __cdecl operator delete(void *)" (??3@YAXPAX@Z) already defined in libcpd.lib(delop.obj)
nafxcwd.lib(thrdcore.obj) : error LNK2001: unresolved external symbol __endthreadex
nafxcwd.lib(thrdcore.obj) : error LNK2001: unresolved external symbol __beginthreadex
Debug/kkSTL.exe : fatal error LNK1120: 2 unresolved externals
Error executing link.exe.

0
 
LVL 30

Expert Comment

by:Axter
Comment Utility
As I stated in previous comment, you need to modify your project settings under C++ TAB and [Code Generation], and change run-time
library to Multithread.
0
 
LVL 4

Author Comment

by:prashant_n_mhatre
Comment Utility
Oops...Your comment was not there when I'd started typing my last comment. I did the same you suggested..still getting the following error..

Linking...
nafxcwd.lib(afxmem.obj) : error LNK2005: "void __cdecl operator delete(void *)" (??3@YAXPAX@Z) already defined in libcpmtd.lib(delop.obj)
LINK : warning LNK4098: defaultlib "LIBCMT" conflicts with use of other libs; use /NODEFAULTLIB:library
Debug/kkSTL.exe : fatal error LNK1169: one or more multiply defined symbols found
Error executing link.exe.


tried using /NODEFAULTLIB -- doesn't work
0
 
LVL 30

Expert Comment

by:Axter
Comment Utility
OK, lets change TRACE to what it should be.
Use OutputDebugString instead for CONSOLE.

And replace "afx.h" with the following includes:
#include <windows.h>
#include <Winbase.h>

Also you can change you project setting back to single thread.
0
 
LVL 4

Author Comment

by:prashant_n_mhatre
Comment Utility
No link error...but no output getting displayed on screen.
0
 
LVL 30

Expert Comment

by:Axter
Comment Utility
#include <windows.h>
#include <Winbase.h>

If you use OutputDebugString, you can't just use it like TRACE, because OutputDebugString, takes a single string, and not a variable argument type.

You can use the code I posted, and just change TRACE to OutputDebugString.

0
 
LVL 30

Expert Comment

by:Axter
Comment Utility
Try createing a new test console application, and paste the following code into the main.cpp file:

#include "stdafx.h"

#include <windows.h>
#include <Winbase.h>


#pragma warning (disable:4786)
#include <sstream>
#include <string>
#include <list>
#include <vector>
#include <deque>
#include <map>

#ifdef _DEBUG
namespace DEBUG_CODE
{
     template<typename T>
     void TraceSTL_Obj(const char* SourceFileName, int SourceLineNum, const char* ObjectName, T t, int Index = -1)
     {
          std::ostringstream oss;
          oss << SourceFileName << "(" << SourceLineNum << ") : {" << ObjectName;
          if (Index > -1) oss << "[" << Index << "]";
          oss << "} = " << t << std::endl;
          OutputDebugString(oss.str().c_str());
     }
     template<typename T>
     void TraceSTL_Container(const char* SourceFileName, int SourceLineNum, const char* ObjectName, T t)
     {
          int Idx = 0;
          for (T::const_iterator i = t.begin();i != t.end();++i, ++Idx)
          {
               std::string szObjectName = ObjectName;
               TraceSTL_Obj(SourceFileName, SourceLineNum, ObjectName, *i, Idx);
          }
     }
     template<typename T>
     void TraceSTL_AsscContainer(const char* SourceFileName, int SourceLineNum, const char* ObjectName, T t)
     {
          int Idx = 0;
          for (T::const_iterator i = t.begin();i != t.end();++i, ++Idx)
          {
               std::string szObjectName = ObjectName;
               TraceSTL_Obj(SourceFileName, SourceLineNum, ObjectName, i->first, Idx);
          }
     }
}
#define TRACE_STL_OBJ(t)              DEBUG_CODE::TraceSTL_Obj(__FILE__, __LINE__, #t, t)
#define TRACE_STL_CONTAINER(t)              DEBUG_CODE::TraceSTL_Container(__FILE__, __LINE__, #t, t)
#define TRACE_STL_ASSC_CONTAINER(t)              DEBUG_CODE::TraceSTL_AsscContainer(__FILE__, __LINE__, #t, t)
#else //_DEBUG
#define TRACE_STL_OBJ(t)            
#define TRACE_STL_CONTAINER(t)            
#define TRACE_STL_ASSC_CONTAINER(t)
#endif //_DEBUG


int main(int argc, char* argv[])
{
     std::vector<std::string> MyStringVect;
     MyStringVect.push_back("Test1");
     MyStringVect.push_back("Test2");
     MyStringVect.push_back("Test3");
     MyStringVect.push_back("Test4");
     TRACE_STL_CONTAINER(MyStringVect);

     std::list<std::string> MyStringList;
     MyStringList.push_back("Testx1");
     MyStringList.push_back("Testx2");
     MyStringList.push_back("Testx3");
     TRACE_STL_CONTAINER(MyStringList);

     std::map<int, std::string> MyStringMap;
     MyStringMap[69] = "Test69";
     MyStringMap[77] = "Test77";
     MyStringMap[33] = "Test33";
     TRACE_STL_ASSC_CONTAINER(MyStringMap);

     std::string test1 = "Hello World! from std::string";
     TRACE_STL_OBJ(test1);

     
     system("pause");
     return 0;
}

0
 
LVL 30

Accepted Solution

by:
Axter earned 100 total points
Comment Utility
I just tested the above code.
You'll see the out put in your debug window.  Make sure you have the debug tab selected in your output window.
0
 
LVL 30

Expert Comment

by:Axter
Comment Utility
Or you can modify your code to the following:
for(Issues = m_IssueList.begin(); Issues != m_IssueList.end(); ++Issues)//You should practice using prefix
   {
        //cerr << *Issues << endl;
        OutputDebugString(Issues->c_str());
   }
0
 
LVL 49

Expert Comment

by:DanRollins
Comment Utility
The simplest way to see it in the Watch window would look like this:

char* pTmp;
for(Issues = m_IssueList.begin(); Issues != m_IssueList.end(); Issues++)
    {
         pTmp= Issues->c_str();
    }

Then put a watch on pTmp.  Or just rest the mouse over it and an infotip will pop up!  You could even use:

     #if _DEBUG
            pTmp= Issues->c_str();
     #endif

so that it won't interfere with the release build.

-- Dan
0
 
LVL 4

Author Comment

by:prashant_n_mhatre
Comment Utility
Thank you all !!!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now