English 中文(简体)
是否有办法了解C++物体的计算及其操作过程的类型?
原标题:Is there a way to know the C++ objects count and their types of a running process?

The memory footprint of one of the processes that runs on Windows only is using more memory than before and I want to know which all C++ objects are in memory to see if there are extra allocations? I can use WinDbg for .NET applications to find it out. But is it possible to do it for C++ without buying a memory profiler commercial application?

问题回答

如果你不能拿不出工具,你就能够使用混合代码,在你喜欢的课堂上添加记号(你可能希望将产出到日志档案中)。 如果你提出问题,我现在就没有时间了,我稍后会回头。

#include <utility>
#include <iostream>
#include <atomic>
#include <string_view>

class report_instance_count_itf
{
public:
    virtual void report_construction(std::string_view type_name, std::size_t number_of_instances) const noexcept = 0;
    virtual void report_destruction(std::string_view type_name, std::size_t number_of_instances) const noexcept = 0;
};

class cout_reporter_t : public report_instance_count_itf
{
public:
    void report_construction(std::string_view type_name, std::size_t number_of_instances) const noexcept override
    {
        std::cout << "construct of class `" << type_name << "` number of instances = " << number_of_instances << "
";
    }

    void report_destruction(std::string_view type_name, std::size_t number_of_instances) const noexcept override
    {
        std::cout << "destruction of class `" << type_name << "` number of instances = " << number_of_instances << "
";
    }
};

//------------------------------------------------------------------------------
// meyers singleton for now (ideally inject)
const report_instance_count_itf& get_instance_count_reporter()
{
    static cout_reporter_t reporter;
    return reporter;
}

//------------------------------------------------------------------------------
// Reusable instance counter implementation,
// make it a class template so we get a unique "instance" per
// class type that uses it. 
// put this class template in its own header file

template<typename class_t>
class instance_counter_impl_t
{
public:
    instance_counter_impl_t()
    {
        ++m_instance_count;
        const std::type_info& ti = typeid(class_t);
        get_instance_count_reporter().report_construction(ti.name(), m_instance_count);
    }

    ~instance_counter_impl_t()
    {
        --m_instance_count;
        const std::type_info& ti = typeid(class_t);
        get_instance_count_reporter().report_destruction(ti.name(), m_instance_count);
    }

    const auto instance_count() const noexcept
    {
        return m_instance_count;
    }

private:
    static std::atomic<std::size_t> m_instance_count;
};

template<typename class_t>
std::atomic<std::size_t> instance_counter_impl_t<class_t>::m_instance_count{};

// End of header file 
//------------------------------------------------------------------------------

// inherit each class you want to count the number of instaces of
// from the class template specialized for that class. 
enter code here
class A :
    public instance_counter_impl_t<A>
{
};

class B :
    public instance_counter_impl_t<B>
{
};

int main()
{
    {
        A a1;
        B b1;
        A a2;
        B b2;
    }

    return 0;
}




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签