【问题标题】:Using PPL with an operator new override将 PPL 与 operator new 覆盖一起使用
【发布时间】:2014-09-04 17:11:26
【问题描述】:

我有一个大型(百万行)VC++ 项目,并想开始在项目中使用 PPL(并行模式库)。该项目实现了自定义全局运算符 new 和 operator delete 功能。问题是 PPL(在调试版本中)使用自己的

void* operator new[](size_t cb, int nBlockUse, const char* szFileName, int nLine)

但是当它取消分配时,我的 operator delete(void*) 被调用,并且由于内存布局完全不同,这不起作用。

我的问题是,是否有办法在调试版本中使用 PPL 而不会放弃我的全局 new 和 delete 运算符。拥有这些全局新和删除覆盖是有充分理由的,目前不能将它们从我的项目中删除。

【问题讨论】:

    标签: visual-c++ ppl


    【解决方案1】:

    这是 Visual C++ 2010 中 PPL 中的一个错误;它已在 Visual C++ 2012 中修复。

    您可以通过编写自己的替换调试运算符 new 和 delete 来解决此问题,这些运算符调用您自己的自定义运算符 new 和 delete:

    void __cdecl operator delete(
        void*       block,
        int const   block_use,
        char const* file_name,
        int const   line_number
        )
    {
        return operator delete(block);
    }
    
    void __cdecl operator delete[](
        void*       block,
        int const   block_use,
        char const* file_name,
        int const   line_number
        )
    {
        return operator delete[](block);
    }
    
    void* __cdecl operator new(
        size_t const size,
        int const    block_use,
        char const*  file_name,
        int const    line_number
        )
    {
        return operator new(size);
    }
    
    void* __cdecl operator new[](
        size_t const size,
        int const    block_use,
        char const*  file_name,
        int const    line_number
        )
    {
        return operator new[](size);
    }
    

    【讨论】:

    • 对不起,我应该提到我正在使用 VS2013。当我尝试覆盖 void* __cdecl operator new[]( size_t const size, int const block_use, char const* file_name, int const line_number ) 时,链接器报告 LIBCMTD.lib(dbgnew.obj) : error LNK2005: "void * __cdecl operator new[](unsigned __int64,int,char const *,int)" (??_U@YAPEAX_KHPEBDH@Z) 已在 UDMemory.obj 中定义 /FORCE:MULTIPLE 链接器选项也不能解决问题。
    猜你喜欢
    • 2012-01-05
    • 1970-01-01
    • 2014-10-08
    • 2019-05-05
    • 1970-01-01
    • 1970-01-01
    • 2013-03-07
    • 2013-07-03
    • 1970-01-01
    相关资源
    最近更新 更多