문제

I am profiling a C++ application compiled on optimization level -O3 with the intel c++ compiler from intel composer xe 2013. The profiler (Instruments on OS X) states that a very large portion of time is being spent calling the destructor for a particular type of object. However, it will not provide me with information regarding what function allocated the object in the first place. Is there any tool that can provide the information on what functions allocate the largest quantity of a certain type of object?

Edit: I have also tried the -profile-functions flag for the intel c++ compiler with no success.

도움이 되었습니까?

해결책

You could add two more parameters to the constructor, the file and the line number. Save that information in the object, and print it when the destructor is called. Optionally you could hide some of the ugliness in a macro for the constructor.

#include <iostream>
#include <string>
using std::string;

class Object
{
    string _file;
    int _line;
  public:
    Object( const char * file, int line ) : _file(file), _line(line) {}
   ~Object() { std::cerr << "dtor for object created in file: " << _file << " line: " << _line << std::endl; }
};

int main( int argc, char * argv[] )
{
    Object obj( __FILE__, __LINE__ );
    return 0;
}

This is how it runs

$ g++ main.cpp -o main && ./main
dtor for object created in file: main.cpp line: 16
$
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top