在我的库中(替换全局new
/ delete
运算符),我使用另一个外部库(casablanca pplx)。
此外部库不包含已替换的new
/ delete
运算符(我使用Visual Studio的show includes选项进行检查)。
此外部库的某些对象(如流或例外)是使用默认的new[]
运算符(调用我已替换的new
运算符)< / em>,并使用默认的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.0\VC\crt\src\newaop.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.0\VC\crt\src\dbgdel.cpp
msvcr110d.dll!operator delete[](void * p) Line 22 C++ // C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\crt\src\delete2.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
new和delete运算符:
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);
答案 0 :(得分:0)
在Visual Studio 2013 (及更早版本)中,默认的delete[]
运算符不是在应用程序的二进制文件中构建的(而不是默认的new
,new[]
和delete
运营商)。它直接从msvcr110d.dll调用。
当使用内嵌关键字时,替换的new[]
和delete[]
运算符不会在整个应用程序中用作默认值(而不是{{ 1}}和new
运算符)除非我们明确包含标题
因此,默认的delete
运算符(内置在应用程序的二进制文件中)会调用替换的new[]
运算符。
但是默认的new
运算符(直接从msvcr110d.dll调用)找不到替换的delete[]
运算符并使用默认的delete
运算符(来自msvcr110d.dll)。
对于我的解决方案,我删除了内嵌关键字并将操作符放在源文件中。
此错误已得到修复,不应出现在Visual Studio 2015 (及更新版)中。