新/删除不匹配
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了新/删除不匹配相关的知识,希望对你有一定的参考价值。
在我的库(替换全局new
/ delete
操作符)中,我使用另一个外部库(casablanca pplx)。这个外部库不包括替换的new
/ delete
操作符(我通过使用Visual studio的show includes选项进行检查)。
此外部库的某些对象(如流或异常)是使用默认的new[]
运算符(调用我替换的new
运算符)创建的,并使用默认的delete[]
运算符(不会调用我替换的delete
运算符)来销毁。这导致一般保护错误。
有什么可以解释这种行为?
我使用Visual Studio 2012。
示例源代码(使用casablanca http客户端获取google.fr):
web::http::client::http_client_config config;
config.set_timeout(std::chrono::seconds(10));
utility::string_t uri = L"http://www.google.fr";
web::http::client::http_client client(uri, config);
web::http::http_request request(web::http::methods::GET);
client.request(request).then([uri](web::http::http_response response ) {
unsigned short statusCode = response.status_code();
if (statusCode == 200)
{
concurrency::streams::stringstreambuf stream;
return response.body().read_to_end(stream).then([stream](size_t) {
std::string responseText = stream.collection();
}); // crash here (stream destructor)
}
});
新的callstack:
Application64_d.dll!operator new(unsigned __int64 iSize) Line 262 C++ // My replaced new operator
Application64_d.dll!operator new[](unsigned __int64 count) Line 7 C++ // C:Program Files (x86)Microsoft Visual Studio 11.0VCcrtsrc
ewaop.cpp
Application64_d.dll!Concurrency::streams::details::basic_producer_consumer_buffer<unsigned char>::_block::_block(unsigned __int64 size) Line 437 C++
Application64_d.dll!std::_Ref_count_obj<Concurrency::streams::details::basic_producer_consumer_buffer<unsigned char>::_block>::_Ref_count_obj<Concurrency::streams::details::basic_producer_consumer_buffer<unsigned char>::_block><unsigned __int64 & __ptr64>(unsigned __int64 & _V0) Line 873 C++
Application64_d.dll!std::make_shared<Concurrency::streams::details::basic_producer_consumer_buffer<unsigned char>::_block,unsigned __int64 & __ptr64>(unsigned __int64 & _V0) Line 972 C++
Application64_d.dll!Concurrency::streams::details::basic_producer_consumer_buffer<unsigned char>::_alloc(unsigned __int64 count) Line 165 C++
Application64_d.dll!Concurrency::streams::details::streambuf_state_manager<unsigned char>::alloc(unsigned __int64 count) Line 651 C++
Application64_d.dll!Concurrency::streams::streambuf<unsigned char>::alloc(unsigned __int64 count) Line 993 C++
Application64_d.dll!web::http::client::details::winhttp_client::completion_callback(void * hRequestHandle, unsigned __int64 context, unsigned long statusCode, void * statusInfo, unsigned long statusInfoLength) Line 1167 C++
winhttp.dll!00007ff83259db67() Unknown
winhttp.dll!00007ff83259d87a() Unknown
winhttp.dll!00007ff832599fe5() Unknown
Application64_d.dll!web::http::client::details::winhttp_client::read_next_response_chunk(web::http::client::details::winhttp_request_context * pContext, unsigned long bytesRead, bool firstRead) Line 693 C++
Application64_d.dll!web::http::client::details::winhttp_client::completion_callback(void * hRequestHandle, unsigned __int64 context, unsigned long statusCode, void * statusInfo, unsigned long statusInfoLength) Line 1156 C++
winhttp.dll!00007ff83259db67() Unknown
winhttp.dll!00007ff8325c653d() Unknown
Application64_d.dll!web::http::client::details::winhttp_client::completion_callback(void * hRequestHandle, unsigned __int64 context, unsigned long statusCode, void * statusInfo, unsigned long statusInfoLength) Line 1051 C++
winhttp.dll!00007ff83259db67() Unknown
winhttp.dll!00007ff8325a20b7() Unknown
winhttp.dll!00007ff8325958a0() Unknown
winhttp.dll!00007ff832594699() Unknown
ntdll.dll!00007ff839723021() Unknown
ntdll.dll!00007ff839721989() Unknown
kernel32.dll!00007ff8392c2774() Unknown
ntdll.dll!00007ff839750d51() Unknown
删除callstack:
msvcr110d.dll!operator delete(void * pUserData) Line 52 C++ // C:Program Files (x86)Microsoft Visual Studio 11.0VCcrtsrcdbgdel.cpp
msvcr110d.dll!operator delete[](void * p) Line 22 C++ // C:Program Files (x86)Microsoft Visual Studio 11.0VCcrtsrcdelete2.cpp
Application64_d.dll!Concurrency::streams::details::basic_producer_consumer_buffer<unsigned char>::_block::~_block() Line 443 C++
Application64_d.dll!Concurrency::streams::details::basic_producer_consumer_buffer<unsigned char>::_block::`scalar deleting destructor'(unsigned int) C++
Application64_d.dll!std::_Ref_count_obj<Concurrency::streams::details::basic_producer_consumer_buffer<unsigned char>::_block>::_Destroy() Line 885 C++
Application64_d.dll!std::_Ref_count_base::_Decref() Line 121 C++
Application64_d.dll!std::_Ptr_base<Concurrency::streams::details::basic_producer_consumer_buffer<unsigned char>::_block>::_Decref() Line 347 C++
Application64_d.dll!std::shared_ptr<Concurrency::streams::details::basic_producer_consumer_buffer<unsigned char>::_block>::~shared_ptr<Concurrency::streams::details::basic_producer_consumer_buffer<unsigned char>::_block>() Line 624 C++
Application64_d.dll!std::shared_ptr<Concurrency::streams::details::basic_producer_consumer_buffer<unsigned char>::_block>::`scalar deleting destructor'(unsigned int) C++
Application64_d.dll!std::allocator<std::shared_ptr<Concurrency::streams::details::basic_producer_consumer_buffer<unsigned char>::_block> >::destroy<std::shared_ptr<Concurrency::streams::details::basic_producer_consumer_buffer<unsigned char>::_block> >(std::shared_ptr<Concurrency::streams::details::basic_producer_consumer_buffer<unsigned char>::_block> * _Ptr) Line 624 C++
Application64_d.dll!std::allocator_traits<std::allocator<std::shared_ptr<Concurrency::streams::details::basic_producer_consumer_buffer<unsigned char>::_block> > >::destroy<std::shared_ptr<Concurrency::streams::details::basic_producer_consumer_buffer<unsigned char>::_block> >(std::allocator<std::shared_ptr<Concurrency::streams::details::basic_producer_consumer_buffer<unsigned char>::_block> > & _Al, std::shared_ptr<Concurrency::streams::details::basic_producer_consumer_buffer<unsigned char>::_block> * _Ptr) Line 758 C++
Application64_d.dll!std::_Wrap_alloc<std::allocator<std::shared_ptr<Concurrency::streams::details::basic_producer_consumer_buffer<unsigned char>::_block> > >::destroy<std::shared_ptr<Concurrency::streams::details::basic_producer_consumer_buffer<unsigned char>::_block> >(std::shared_ptr<Concurrency::streams::details::basic_producer_consumer_buffer<unsigned char>::_block> * _Ptr) Line 910 C++
Application64_d.dll!std::deque<std::shared_ptr<Concurrency::streams::details::basic_producer_consumer_buffer<unsigned char>::_block>,std::allocator<std::shared_ptr<Concurrency::streams::details::basic_producer_consumer_buffer<unsigned char>::_block> > >::pop_front() Line 1476 C++
Application64_d.dll!Concurrency::streams::details::basic_producer_consumer_buffer<unsigned char>::update_read_head(unsigned __int64 count) Line 644 C++
Application64_d.dll!Concurrency::streams::details::basic_producer_consumer_buffer<unsigned char>::read(unsigned char * ptr, unsigned __int64 count, bool advance) Line 620 C++
Application64_d.dll!<lambda_1f1656160f66eb3ff369c30cc6352caf>::operator()() Line 279 C++
Application64_d.dll!std::_Callable_obj<<lambda_1f1656160f66eb3ff369c30cc6352caf>,0>::_ApplyX<void>() Line 431 C++
Application64_d.dll!std::_Func_impl<std::_Callable_obj<<lambda_1f1656160f66eb3ff369c30cc6352caf>,0>,std::allocator<std::_Func_class<void,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil> >,void,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil>::_Do_call() Line 239 C++
Application64_d.dll!std::_Func_class<void,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil>::operator()() Line 514 C++
Application64_d.dll!Concurrency::streams::details::basic_producer_consumer_buffer<unsigned char>::_request::complete() Line 543 C++
Application64_d.dll!Concurrency::streams::details::basic_producer_consumer_buffer<unsigned char>::fulfill_outstanding() Line 424 C++
Application64_d.dll!Concurrency::streams::details::basic_producer_consumer_buffer<unsigned char>::_close_write() Line 360 C++
Application64_d.dll!Concurrency::streams::details::streambuf_state_manager<unsigned char>::close(int mode) Line 413 C++
Application64_d.dll!Concurrency::streams::streambuf<unsigned char>::close(int mode) Line 960 C++
Application64_d.dll!Concurrency::streams::basic_ostream<unsigned char>::close() Line 151 C++
Application64_d.dll!web::http::details::http_msg_base::_complete(unsigned __int64 body_size, const std::exception_ptr & exceptionPtr) Line 181 C++
Application64_d.dll!web::http::client::details::request_context::complete_request(unsigned __int64 body_size) Line 119 C++
Application64_d.dll!web::http::client::details::winhttp_client::completion_callback(void * hRequestHandle, unsigned __int64 context, unsigned long statusCode, void * statusInfo, unsigned long statusInfoLength) Line 1194 C++
winhttp.dll!00007ff83259db67() Unknown
winhttp.dll!00007ff83259d87a() Unknown
winhttp.dll!00007ff832599fe5() Unknown
Application64_d.dll!web::http::client::details::winhttp_client::read_next_response_chunk(web::http::client::details::winhttp_request_context * pContext, unsigned long bytesRead, bool firstRead) Line 693 C++
Application64_d.dll!web::http::client::details::winhttp_client::completion_callback(void * hRequestHandle, unsigned __int64 context, unsigned long statusCode, void * statusInfo, unsigned long statusInfoLength) Line 1231 C++
winhttp.dll!00007ff83259b6aa() Unknown
Application64_d.dll!web::http::client::details::winhttp_client::completion_callback(void * hRequestHandle, unsigned __int64 context, unsigned long statusCode, void * statusInfo, unsigned long statusInfoLength) Line 1174 C++
winhttp.dll!00007ff83259db67() Unknown
winhttp.dll!00007ff8325955e7() Unknown
winhttp.dll!00007ff832594699() Unknown
ntdll.dll!00007ff839723021() Unknown
ntdll.dll!00007ff839721989() Unknown
kernel32.dll!00007ff8392c2774() Unknown
ntdll.dll!00007ff839750d51() Unknown
新建和删除运营商:
inline void* operator new (::size_t size);
inline void operator delete (void* ptr);
inline void* operator new[] (::size_t size);
inline void operator delete[] (void* ptr);
在Visual Studio 2013(及更早版本)中,默认的delete[]
运算符不是在应用程序的二进制文件中构建的(与默认的new
,new[]
和delete
运算符相对)。它直接从msvcr110d.dll调用。
当使用inline关键字时,替换的new[]
和delete[]
运算符在整个应用程序中不用作默认值(与new
和delete
运算符相对),除非我们明确包含标题。
所以默认的new[]
运算符(内置在应用程序的二进制文件中)调用替换的new
运算符。
但默认的delete[]
运算符(直接从msvcr110d.dll调用)找不到替换的delete
运算符并使用默认的delete
运算符(来自msvcr110d.dll)。
对于我的解决方案,我删除了inline关键字并将运算符放在源文件中。 此错误已得到修复,不应出现在Visual Studio 2015(及更新版本)中。
以上是关于新/删除不匹配的主要内容,如果未能解决你的问题,请参考以下文章