我正在 Centos 平台上开发共享库和应用程序 [clang++、llvm3.9.0 和 libc++],库和应用程序都重载了自己的运算符 new 和运算符删除。
除了 1 例外,一切正常。在调用 std::string 的复制构造函数时,总是调用应用程序端的运算符 new:
这是塞纳里奥:
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 new in shared library
operator new called Application side
operator delete in shared library
operator delete in shared library
====================================================
共享库端运算符新建和删除:
void * operator new ( size_t len ) throw ( std::bad_alloc )
{
printf("operator new in shared libraryn");
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 libraryn");
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 libraryn");
if ( ptr != 0 ) free ( ptr );
}
void operator delete[] ( void * ptr ) throw()
{
printf("operator delete[] in shared libraryn");
if ( ptr != 0 ) free ( ptr );
}
应用程序端运算符新建和运算符删除:
void * operator new ( size_t len ) throw ( std::bad_alloc )
{
void * mem = malloc ( len );
printf("operator new called Application siden");
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 siden");
if ( (mem == 0) && (len != 0) ) throw std::bad_alloc();
return mem;
}
void operator delete ( void * ptr ) throw()
{
printf("operator delete[] called Application siden");
if ( ptr != 0 )free ( ptr );
}
void operator delete[] ( void * ptr ) throw()
{
printf("operator delete[] called Application siden");
if ( ptr != 0 ) free ( ptr );
}
请帮忙。
简短的回答:不要那样做。
应该只有一个替换operator new
(好吧,有一堆口味,如noexcept
和[]
等,但每种口味只有一个)。
如果你有多个,那么 - 正如你所发现的 - 不清楚哪一个被调用,你可能会得到不匹配的调用新的和删除,这是对未定义行为的快速访问。
我可以解释为什么你会得到你报告的确切行为,但它与内联和外部模板有关,这并不重要。您有两个用于operator new
的替换功能,这就是问题所在。