使用新宏的c ++ std :: make_shared

时间:2014-09-20 13:45:05

标签: c++ c++11 make-shared

我使用宏代替new来获取调试模式中的一些额外信息:

#if defined(_DEBUG)
#define SAGE_NEW new(__FUNCTION__, __FILE__, __LINE__)
#else
#define SAGE_NEW new
#endif

我发现这在自定义内存分析和内存泄漏检测中非常有用。我刚开始使用共享指针,所以现在我正在创建堆对象,如:

auto myThingy = std::shared_ptr<Thingy>(SAGE_NEW Thingy(Args) );

我刚刚了解到std::make_shared是首选,因为它使用较少的内存分配。我有什么办法可以将SAGE_NEW加入make_shared吗?我意识到它对于泄漏检测不重要,但我仍然希望它用于内存使用统计。似乎allocate_shared以某种方式得出了答案,但我还没有弄明白。谢谢! :)

修改: 对于那些询问new的人 - 我使用自定义new重载它。编译器选项SAGE_MEM_INFO打开泄漏检测和内存使用情况统计信息,否则会跳过日志记录并直接进入内存池分配。我有新的[]和删除[]变体,但我为了简洁起见省略了这些:

#ifdef SAGE_MEM_INFO
void* operator new  (size_t size){ ++appAllocs; return myAlloc(size); }
void* operator new  (size_t size, char const *function, char const *filename, int lineNum)
{
    ... Log memory usage
    void* ptr = ::operator new(size);
    return ptr;
}
void  operator delete   (void* ptr)
{
    ... Log freeing of this pointer
    --appAllocs;
    myFree(ptr);
}
void  operator delete   (void* ptr, char const *function, char const *filename, int lineNum)
{
    ... Log freeing of this pointer
    --appAllocs;
    myFree(ptr);
}
#else
void* operator new  (size_t size){ return myAlloc(size); }
void* operator new  (size_t size, char const *function, char const *filename, int lineNum)
{
    void* ptr = ::operator new(size);
    return ptr;
}
void  operator delete  (void* ptr) { myFree(ptr); }
void  operator delete  (void* ptr, char const *function, char const *filename, int lineNum) { myFree(ptr); } 
#endif

3 个答案:

答案 0 :(得分:1)

是的,你当然可以这样做。

但是,你必须选择你的毒药:

  • 使用非空的allocator-type,但至少保存一个指针。
  • 为每个分配使用新的allocator-type,它将反映在引用计数的新多态类型中。

http://en.cppreference.com/w/cpp/concept/Allocator显示了需求,以及一个很好的最小分配器声明。

在此处针对第一个选项改编std::allocator

#if defined(_DEBUG)
template <class Tp>
struct DebugLinesAllocator : std::allocator<Tp> {
  const char* func, *file;
  int line;
  Tp* allocate(std::size_t n, const void* = 0)
  {return ::operator new(n * sizeof(T), func, file, line);}
  template< class U > struct rebind { typedef DebugLinesAllocator<U> other; };
  DebugLinesAllocator(const char* func, const char* file, int line)
  : func(func), file(file), line(line) {}
  template< class U > DebugLinesAllocator( const DebugLinesAllocator<U>& other )
  : func(other->func), file(other->file), line(other->line) {}
};
#define SAGE_MAKE_SHARED(type, ...) allocate_shared<type>(DebugLinesAllocator<type>\
    {__FUNCTION__, __FILE__, __LINE__}, __VA_ARGS__)
#else
#define SAGE_MAKE_SHARED(type, ...) make_shared<type>(__VA_ARGS__)
#endif

尽管如此,它对共享指针的用处却少得多。无论如何,每一点都可能有所帮助。

一样使用它
auto p = SAGE_MAKE_SHARED(my_type, my_argument_1, ...);

答案 1 :(得分:0)

您可以创建自己的makeShared,在那里您可以执行所需的任何记账,之后您将拨打真实make_shared

答案 2 :(得分:0)

在答案中发帖而不是问题......

我的编译器并不喜欢Deduplicator的答案中的一些内容。以下似乎并非完全错误:

template <class Tp>
struct DebugLinesAllocator : std::allocator<Tp> {
    char const * func;
    char const * file;
    int line;
    DebugLinesAllocator(char const * aFunc, char const * aFile, const int aLine) : func(aFunc), file(aFile), line(aLine) {}
    Tp* allocate(std::size_t n, const void* = 0)
    {
        return static_cast<Tp*> (::operator new(n * sizeof(Tp), func, file, line));
    }
    template< class U > struct rebind { typedef DebugLinesAllocator<U> other; };
    template< class U > DebugLinesAllocator(const DebugLinesAllocator<U>& other)
        : func(other.func), file(other.file), line(other.line) {}
};
#if defined(_DEBUG)
#define SAGE_MAKE_SHARED(type, ...) std::allocate_shared<type>(DebugLinesAllocator<type>(__FUNCTION__, __FILE__, __LINE__), __VA_ARGS__)
#else
#define SAGE_MAKE_SHARED(type, ...) std::make_shared<type>(__VA_ARGS__)
#endif

如果您认为我的任何更改都是可疑的,请告诉我。

相关问题