dumping content of an object

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.
LVL 4
prashant_n_mhatreAsked:
Who is Participating?
 
AxterCommented:
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
 
DanRollinsCommented:
>> 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
 
AxterCommented:
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
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
xLsCommented:
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
 
AxterCommented:
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
 
AxterCommented:
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
 
snoeglerCommented:
0
 
snoeglerCommented:
Ooops sorry i didn't see the last line in your question :)
0
 
prashant_n_mhatreAuthor Commented:
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
 
AxterCommented:
Use the STL trace code I posted above.
TRACE_STL_ASSC_CONTAINER(m_IssueList);


TRACE_STL_OBJ(*Issues);
0
 
AxterCommented:
Correction:
TRACE_STL_CONTAINER(m_IssueList);


TRACE_STL_OBJ(*Issues);
0
 
AxterCommented:
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
 
prashant_n_mhatreAuthor Commented:
TRACE doesn't work in console application. Do I need to write my own TRACE macro?

I guess simple 'printf' will work.
0
 
AxterCommented:
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
 
AxterCommented:
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
 
prashant_n_mhatreAuthor Commented:
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
 
AxterCommented:
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
 
prashant_n_mhatreAuthor Commented:
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
 
AxterCommented:
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
 
prashant_n_mhatreAuthor Commented:
No link error...but no output getting displayed on screen.
0
 
AxterCommented:
#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
 
AxterCommented:
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
 
AxterCommented:
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
 
DanRollinsCommented:
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
 
prashant_n_mhatreAuthor Commented:
Thank you all !!!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.