• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 432
  • Last Modified:

log the "call stack"

I am fairly new at C++. I was just assigned to this system 1 week ago.  
At work one of our customers is having problems with our dll.  
They are using our COM version of the dll with ASP pages.  After a few minutes of using the ASP app by a few users that app hangs.
This is only happening with the latest version of the dll. The previous version works fine for the customer. The new version has a lot of changes on  a custom
String class. I think this class is causing the problem (memory leak probably). Changes on this String class were made to solve some reporting issues. the reporting issues were fixed but this particular customer cannot use the new version.

We cannot recreate the problem in our site so its very difficult to debug.

I was thinking to add some code to the program so it prints to a log file when it encounters a prblem. Hopefully I can print the call stack when an error occurs so I can have a better idea where the problem could be.
I was wondering if this is possible and if somebody has an example on how to do this.??       The dll is coded on c++ (Microsoft Visual C++)
I am not sure of other way I can troubleshoot the problem.

  • 5
  • 2
  • 2
  • +4
3 Solutions
Check out http://win32.mvps.org/misc/stackwalk.html ("StackWalk") and http://www.codeproject.com/tools/leakfinder.asp ("Memory(-Leak) and Exception Trace (CRT and COM Leaks)"). The latter allows you to write your own exception filter and just call StackwalkFilter to produce the callstack.

I would probably create a simple log class , declare it the beggining of each function, at the constructor would insert an entry to a global stack, and at the destructor, will remove that entry, this will create an image of the call-stack, but you can actually insert more than just the name of the function, you may enter the parameters of the function, or some variables, so you'll know better what was going on.
Check out the free logging code in the following link:

With the above logging code, you can use the EZLOGGERFUNCTRACKER macro to track function entry and exit.
Example usage:

void FunctFoo()
      //Function Foo code here....

If you're using VC++ 7.x, 8.0, GNU 3.x, or 4.0, the logging code will automatically pickup the function name.
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.

chicho12Author Commented:
Can you show me an example of how insert an entry to a global stack on the constructor?  and removing it

>>I would probably create a simple log class , declare

If you don't know what you're doing, a logging class can add more bugs to your code, and you end up spending more time debugging your logging class, than you do debugging your real problem.

Global logging classes are not simple when used in multiple source files (*.cpp), and when you need to call them from global or static objects.
When calling a global loggging class from a global object's constructor, you have to worry about the order of initialization.

If there are no calls to the global logging class before main(), than the task can be simple.
basicly the links jkr and Axter gave you will do the same, but in more details

use the std::vector and use .push_back() at the constructor and .pop_back() at the destructor

the vector can be either global variable, or a static member of your logger class, it's up to you how to define it....

p.s. I would define a macro to declare the class
something like
#define STACK_LOG stackLog Logger ;
#define STACK_LOG

and then you can add the STACK_LOG to your code, and just by removing the define USE_STACK_LOG it will be removed from the code, and will not interfere with the release version...

good luck
I'd rather not change the code at large scale if not necessary...
i think You can acheive this through set_terminate(handler); function.
use american arium emulator which logs call stack automatically . Tool is aldreay available . this works for pentium machines.

Just load your exe and run your code  and see log file automatically it logs all . whenevr exception or something happens it shows clearly the address where it failed . and u can see map file andlocate the function

This is pretty simple all u need to do is get american arium emulator

No need to change any code

>>This is pretty simple all u need to do is get american arium emulator
>>No need to change any code

This may not be possible, if the developer is trying to trouble shoot a problem that only occurs in a customer site.
And that's what the questioner is stating.
If you are pretty sure that it's memory leak set new handler,
Override new and delete, or try to print memory address and count in string or other particular class  constructor and destructor.

Overriding new and delete [class constructor and destructor] solves most of my memory leak problem.

If you think it's a memory leak problem, you can try using the following free leak detection code:

You'll need the DLL that you can download from following link:

Using this code, you don't even need to change your source code, and you can use it to report memory leaks.
Instructions are in the leaktracker.h header.
why cant he take emulator and connect it in customer site for debugging ?  some one aldready have solution just use it !
>>why cant he take emulator and connect it in customer site for debugging ?  some one aldready have solution just use it !

Is this a free program?  If so, do you have a link for it?

If not, than in general, you just can't throw license programs on any customer's computer.
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.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

  • 5
  • 2
  • 2
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now