简体   繁体   English

C ++对象生命周期分析

[英]C++ object lifetime profiling

ObjectInfo class is a diagnostic class intended to track statistical data such as life time and number of objects. ObjectInfo类是诊断类,旨在跟踪统计数据,例如寿命和对象数量。 A specific class inherits from ObjectInfo as shown. 如图所示,特定的类从ObjectInfo继承。 A member of that specific class is then declared in the body of a profiled class. 然后,在概要分析的类的主体中声明该特定类的成员。

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. 扩展分析类以收集不同的信息(例如对象的大小)也将很困难。

Propose a better solution where the dependencies between the profiled and profiling classes are minimal. 提出一个更好的解决方案,使配置文件和配置文件类之间的依赖性最小。

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;
};

I originally thought this question is asking about using C++ reflection technique to gather the runtime information. 我最初以为这个问题是关于使用C ++反射技术来收集运行时信息的问题。 However, I don't know if there is a way to measure the lifetime of objects using C++ reflection. 但是,我不知道是否有一种方法可以使用C ++反射来测量对象的寿命。 Furhter, can you consider C++ reflection is a technique that reduces the dependencies between the profiled and profiling classes ? 此外,您是否认为C ++反射是一种可减少配置文件类与性能分析类之间的依赖关系的技术?

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
}

This uses the CRTP that Bartek brought up in the comments on your question. 这将使用Bartek在对您的问题的评论中提出的CRTP。 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. 它还包装了基类的new标准,它由派生类继承,这使我们可以跟踪堆分配。 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). 因此,我们知道创建了多少实例,堆上有多少实例,并且我们可以推断出其余实例在堆栈上(除非您在程序中使用对象池或其他一些更奇特的分配策略)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM