简体   繁体   English

了解在共享库中重载operator new的行为

[英]Understanding behavior of overloading operator new in shared library

I am working on a shared library and application on Centos platform [clang++, llvm3.9.0 and libc++] , and both library and application overload their own operator new and operator delete. 我正在使用Centos平台上的共享库和应用程序[clang ++,llvm3.9.0和libc ++],并且库和应用程序都会重载它们自己的operator new和operator delete。

Everything is working fine except for 1 case. 除1例外,一切正常。 On calling copy constructor of std::string always calling operator new of application side: 在调用std :: string的copy构造函数时总是调用应用程序端的operator new

Here is the senario: 这是senario:

std::string str1 ( "A very strange issue on CentOS using clang and libc++" ); //operator new of library side called.

std::string str2(str1); //operator new of application side called. WHY??

operator delete on both cases are called for library side. 对于库侧调用两种情况下的operator delete

Here are the logs when running the following code: 以下是运行以下代码时的日志:

====================================================
operator new in shared library
operator new called Application side
operator delete in shared library
operator delete in shared library
====================================================

Shared Library side operator new and delete: 共享库侧操作员new和delete:

    void * operator new ( size_t len ) throw ( std::bad_alloc )
    {
        printf("operator new in shared library\n");
        void * mem = malloc( len );
        if ( (mem == 0) && (len != 0) ) throw std::bad_alloc();
        return mem;
    }

    void * operator new[] ( size_t len ) throw ( std::bad_alloc )
    {
        printf("operator new[] in shared library\n");
        void * mem = malloc( len );
        if ( (mem == 0) && (len != 0) ) throw std::bad_alloc();
        return mem;
    }

    void operator delete ( void * ptr ) throw()
    {
        printf("operator delete in shared library\n");
        if ( ptr != 0 ) free ( ptr );
    }

    void operator delete[] ( void * ptr ) throw()
    {
        printf("operator delete[] in shared library\n");
        if ( ptr != 0 ) free ( ptr );
    }

Application side operator new and operator delete: 应用程序端操作员new和operator delete:

void * operator new ( size_t len ) throw ( std::bad_alloc )
{
    void * mem = malloc ( len );
    printf("operator new called Application side\n");
    if ( (mem == 0) && (len != 0) ) throw std::bad_alloc();
        return mem;
}

void * operator new[] ( size_t len ) throw ( std::bad_alloc )
{
    void * mem = malloc ( len );
    printf("operator new[] called Application side\n");
    if ( (mem == 0) && (len != 0) ) throw std::bad_alloc();
        return mem;
}

void operator delete ( void * ptr ) throw()
{
    printf("operator delete[] called Application side\n");
    if ( ptr != 0 )free ( ptr );
}

void operator delete[] ( void * ptr ) throw()
{
    printf("operator delete[] called Application side\n");
    if ( ptr != 0 ) free ( ptr );
}

Please help. 请帮忙。

Short answer: Don't do that. 简短的回答:不要这样做。

There's only supposed to be one replacement operator new (ok, there are a bunch of flavors, like noexcept , and [] , etc, but only one of each flavor). 应该只有一个替换operator new (好吧,有一堆风味,如noexcept[]等,但只有一种风味)。

If you have more than one, then - as you have discovered - it is not clear which one gets called, and you can get mismatched calls to new and delete, which is a quick trip to undefined behavior. 如果您有多个,那么 - 正如您所发现的那样 - 不清楚哪个被调用,并且您可以获得对new和delete的不匹配调用,这是对未定义行为的快速访问。

I could explain why you're getting the exact behavior that you're reporting, but it has to do with inlining, and extern templates, and it doesn't really matter. 我可以解释为什么你得到了你正在报告的确切行为,但它与内联模板和外部模板有关,而且它并不重要。 You have two replacement functions for operator new , and that's the problem. 你有两个替换函数operator new ,这就是问题所在。

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

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