gpt4 book ai didi

c++ - 新建/删除不匹配

转载 作者:行者123 更新时间:2023-12-03 03:47:36 27 4
gpt4 key购买 nike

在我的库(替换全局 new/delete 运算符)中,我使用另一个外部库(casablanca pplx)。此外部库不包含替换的 new/delete 运算符(我使用 Visual Studio 的 showincludes 选项进行了检查)。

此外部库的某些对象(例如流或异常)是使用默认的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)
}
});

新的调用堆栈:

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

删除调用堆栈:

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

新建和删除运算符:

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[] 运算符不是内置在应用程序的二进制文件中(与默认的newnew[]delete 运算符)。它是直接从 msvcr110d.dll 调用的。

当使用inline关键字时,被替换的new[]delete[]运算符在整个应用程序中不会被默认使用(与 newdelete 运算符相反),除非我们明确包含 header 。
因此,默认的 new[] 运算符(内置于应用程序的二进制文件中)调用替换的 new 运算符。
但默认的 delete[] 运算符(直接从 msvcr110d.dll 调用) 找不到替换的 delete 运算符并使用默认的 删除运算符(来自msvcr110d.dll)

对于我的解决方案,我删除了 inline 关键字并将运算符放入源文件中。
此错误已修复,不应出现在 Visual Studio 2015(及更新版本)中。

关于c++ - 新建/删除不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45330453/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com