C ++对象生存期概要分析 [英] C++ object lifetime profiling

查看:225
本文介绍了C ++对象生存期概要分析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ObjectInfo类是一种诊断类,用于跟踪统计数据,例如生命时间和对象数。一个特定的类继承自ObjectInfo,如图所示。

ObjectInfo class is a diagnostic class intended to track statistical data such as life time and number of objects. A specific class inherits from ObjectInfo as shown. A member of that specific class is then declared in the body of a profiled class.

虽然解决方案的工作原理很难维护,因为它需要分析类保持与profiled类同步,因为类名用于标识后者。这也很难扩展profiling类来收集不同的信息,如对象的大小。

Although the solution works it is hard to maintain as it requires the profiling class to be keep in sync with the profiled class as the class name is used to identify the later. It would also be hard to extend the profiling class to gather different information such as the size of object.

提出一个更好的解决方案,其中profiled和profiling类之间的依赖最小。

Propose a better solution where the dependencies between the profiled and profiling classes are minimal.

是否可以实现一个检查来确定是否在堆栈或堆上创建了profiled类的对象?

Is it possible to implement a check that would determine if the object of the profiled class was created on stack or heap?

- ObjectInfo.h -

-- ObjectInfo.h --

#pragma once

class ObjectInfo
{
 public:
  ObjectInfo(const char* objectName);
  virtual ~ObjectInfo(void);

 private:

   static int  m_counter;
   int         m_objectNumber;
   const char* m_className;
};

- ObjectInfo.cpp -

-- ObjectInfo.cpp --

#include "StdAfx.h"
#include "ObjectInfo.h"
#include <iostream>
#include "TimePrinter.h"

using namespace std;
int ObjectInfo::m_counter = 0;
ObjectInfo::ObjectInfo(const char* name) :
m_className(name)
{
   m_objectNumber = ++m_counter;
   cout << "Object: " << m_className << "# " << m_objectNumber << " created @ " <<
   TimePrinter()<< endl;
}

ObjectInfo::~ObjectInfo(void)
{
  cout << "Object: " << m_className << "# " << m_objectNumber << " destroyed @ " << 
  TimePrinter() << endl;
}

- 使用模式 -

-- The use pattern --

struct _AInfo : public ObjectInfo {
    _AInfo() : ObjectInfo("_A") {}
};

struct _A {
  _AInfo m_info;
};



我最初认为这个问题是关于使用C ++反射技术收集运行时信息。但是,我不知道是否有一种方法来测量使用C ++反射的对象的生命周期。但是,你能考虑C ++反射是一种减少简档和性能分析类之间的依赖关系的技术吗?

I originally thought this question is asking about using C++ reflection technique to gather the runtime information. However, I don't know if there is a way to measure the lifetime of objects using C++ reflection. Furhter, can you consider C++ reflection is a technique that reduces the dependencies between the profiled and profiling classes ?

推荐答案

这可以跟踪堆栈对堆栈对象的创建

This can track stack vs. heap object creations

#include <iostream>

template <class CRTP>
struct AllocationTracker
{
    AllocationTracker()
    {
        ++totalCreated;
    }

    void* operator new(size_t sz)
    {
        ++heapCreated;
        return ::operator new(sz);
    }

    static int totalCreated;
    static int heapCreated;
};

template <class CRTP>
int AllocationTracker<CRTP>::totalCreated;
template <class CRTP>
int AllocationTracker<CRTP>::heapCreated;

class Derived : public AllocationTracker<Derived>
{
};

int main()
{
    using namespace std;
    cout << Derived::heapCreated << "/" << Derived::totalCreated << endl; // 0/0
    Derived dStack;
    cout << Derived::heapCreated << "/" << Derived::totalCreated << endl; // 0/1
    Derived* dHeap = new Derived;
    cout << Derived::heapCreated << "/" << Derived::totalCreated << endl; // 1/2
}

这使用了Bartek在评论中提出的CRTP对你的问题。这使我们分别跟踪每个派生类型。它还为基类包装标准 new ,这是由派生类继承的,这允许我们跟踪堆分配。因此,我们知道创建了多少实例,以及在堆上有多少实例,我们可以推断其余实例在栈上(除非您在程序中使用对象池或其他更具异乎寻常的分配策略)。

This uses the CRTP that Bartek brought up in the comments on your question. This lets us track each derived type separately. It also wraps the standard new for the base class, which is inherited by derived classes, which allows us to track heap allocations. So we know how many instances are created, and how many on the heap, and we can infer that the rest were on the stack (unless you're using object pools or some other more exotic allocation strategies in your program).

这篇关于C ++对象生存期概要分析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆