2013-11-15 41 views
0

我在调试第一次访问冲突时出现问题,该冲突是在Windows 8上崩溃了我的桌面应用程序。它尤其令人沮丧,因为在我的Windows 8虚拟机上没有发生崩溃我用于开发,只在平板设备上运行。引导::异常消息的第一次和第二次访问冲突

我具有被如此定义的boost ::例外:

struct ExceptionBase: virtual std::exception, virtual boost::exception {}; 
struct DEVICES_COMMUNICATION_API CDeviceException: virtual ExceptionBase {}; 
typedef boost::error_info<struct tag_device_message, std::string> device_message; 

还有一个内联函数来设置错误消息:

inline void throwDeviceException(std::string message) 
{ 
    CDeviceException de = CDeviceException() 
    << (device_message(std::string(message))); 
    BOOST_THROW_EXCEPTION(de); 
} 

此外另一个内联函数来检索该错误消息:

inline std::string retrieveDeviceExceptionMessage(CDeviceException de) 
{ 
    return *(boost::get_error_info<device_message, CDeviceException>(de)); 
} 

例外被发现在我的应用程序(这是一个服务r),然后分配到使用protobuf生成的类中,然后序列化。映入类中的异常,并将其存储的代码是一个升压线程中运行一个算符:

struct invoke_fn 
{ 
    void operator()(devices::server::CDeviceServer& server, 
        boost::promise<void>&   boostPromise, 
        CDeviceClientRequest&   request, 
        CDeviceServerResponse&   response) 
    { 
     try 
     { 
     server.invoke(request, response); 
     boostPromise.set_value(); 
     } 
     catch (devices::util::CDeviceException &e) 
     { 
     std::string message = devices::util::retrieveDeviceExceptionMessage(e); 
     response.set_errormessage(message); 
     } 
     catch (std::exception &e) 
     { 
     std::string message(e.what()); 
     response.set_errormessage(message); 
     } 
    } 
}; 

以及设定响应对象,这是自动生成由protobuf的错误信息上的代码,是:

inline void CDeviceServerResponse::set_errormessage(const ::std::string& value) { 
    set_has_errormessage(); 
    if (errormessage_ == &::google::protobuf::internal::kEmptyString) { 
    errormessage_ = new ::std::string; 
    } 
    errormessage_->assign(value); 
} 

这使用assign()将传入的字符串复制到protobuf类。服务器请求正在它自己的boost :: thread里面运行,像这样:

boost::promise<void>  boostPromise; 
boost::unique_future<void> boostFuture = boostPromise.get_future(); 
boost::thread    boostThread([&]() 
          { 
           invoke_fn functor; 
           functor(*this, 
             boostPromise, 
             request, 
             response); 
          }); 

return (boostFuture.wait_for(boost::chrono::milliseconds(timeout)) 
     == 
     boost::future_status::ready); 

据我可以告诉这段代码看起来OK。然而下面的栈跟踪已经观察到:

从Dr.Memory:

catch (devices::util::CDeviceException &e) 
{ 
    std::string message = devices::util::retrieveDeviceExceptionMessage(e); 
    response.set_errormessage(message); 
} // HERE 

> ~~3868~~ 
> ~~3868~~ Error #1: UNADDRESSABLE ACCESS: reading 0x03233e70-0x03233e74 4 byte(s) 
> ~~3868~~ # 0 amalgam-devices-communication.dl!std::basic_string<char,std::char_traits<char>,std::allocator<char> >::_Grow [c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstring:2226] 
> ~~3868~~ # 1 amalgam-devices-communication.dl!std::basic_string<char,std::char_traits<char>,std::allocator<char> >::assign [c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstring:1113] 
> ~~3868~~ # 2 amalgam-devices-communication.dl!std::basic_string<char,std::char_traits<char>,std::allocator<char> >::assign [c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstring:1099] 
> ~~3868~~ # 3 amalgam-devices-communication.dl!CDeviceServerResponse::set_errormessage [c:\workspace\bitbbucket\1edit_amalgam\devices\communication\src\main\c++\protobuf\src\main\cdeviceserverresponse.pb.h:294] 
> ~~3868~~ # 4 amalgam-devices-communication.dl!invoke_fn::operator()      [c:\workspace\bitbbucket\1edit_amalgam\devices\communication\src\main\c++\server\cdeviceserver.cc:156] 
> ~~3868~~ # 5 amalgam-devices-communication.dl!<lambda_e4b2ef864a7f24dec10bd7126eadbce1>::operator() [c:\workspace\bitbbucket\1edit_amalgam\devices\communication\src\main\c++\server\cdeviceserver.cc:184] 
> ~~3868~~ # 6 amalgam-devices-communication.dl!boost::detail::thread_data<<lambda_e4b2ef864a7f24dec10bd7126eadbce1> >::run [c:\workspace\bitbbucket\1edit_precompiledheaders\desktopdevices\external-sources\boost\thread\detail\thread.hpp:74] 
> ~~3868~~ # 7 amalgam-devices-communication.dl!boost::`anonymous namespace'::thread_start_function [c:\users\marcusma\source\1edit-prism\boost_1_51_0\libs\thread\src\win32\thread.cpp:190] 
> ~~3868~~ # 8 MSVCR110D.dll!beginthreadex            +0x1a0 (0x73dce001 <MSVCR110D.dll+0x5e001>) 
> ~~3868~~ # 9 MSVCR110D.dll!endthreadex             +0x170 (0x73dce1d1 <MSVCR110D.dll+0x5e1d1>) 
> ~~3868~~ #10 KERNEL32.dll!BaseThreadInitThunk           +0xd  (0x7735850d <KERNEL32.dll+0x2850d>) 
> ~~3868~~ Note: @0:00:44.759 in thread 3868 
> ~~3868~~ Note: next higher malloc: 0x03234000-0x03234030 
> ~~3868~~ Note: 0x03233e70-0x03233e74 overlaps memory 0x03233e58-0x03233e74 that was freed 
> ~~3868~~ Note: instruction: mov 0x18(%eax) -> %ecx 
> ~~3868~~ 

第一和第二访问冲突在第一异常的端部花括号处理catch块发生

并且Visual Studio调试器堆栈跟踪指示在〜CDeviceException()析构函数中发生异常,同时尝试释放在该对象上设置的error_info(device_message)。

在Visual Studio:

First-chance exception at 0x76DA4B32 in amalgam-devices-server.exe: Microsoft C++ exception: boost::exception_detail::clone_impl<boost::broken_promise> at memory location 0x030EF55C. 
First-chance exception at 0x76DA4B32 in amalgam-devices-server.exe: Microsoft C++ exception: boost::exception_detail::clone_impl<boost::broken_promise> at memory location 0x030EF55C. 
First-chance exception at 0x76DA4B32 in amalgam-devices-server.exe: Microsoft C++ exception: [rethrow] at memory location 0x00000000. 
First-chance exception at 0x76DA4B32 in amalgam-devices-server.exe: Microsoft C++ exception: [rethrow] at memory location 0x00000000. 
First-chance exception at 0x76DA4B32 in amalgam-devices-server.exe: Microsoft C++ exception: boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::system::system_error> > at memory location 0x031EF6C0. 
First-chance exception at 0x76DA4B32 in amalgam-devices-server.exe: Microsoft C++ exception: boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::system::system_error> > at memory location 0x031EF6C0. 
First-chance exception at 0x76DA4B32 in amalgam-devices-server.exe: Microsoft C++ exception: boost::exception_detail::clone_impl<devices::util::CDeviceException> at memory location 0x031EF6A8. 
First-chance exception at 0x76DA4B32 in amalgam-devices-server.exe: Microsoft C++ exception: boost::exception_detail::clone_impl<devices::util::CDeviceException> at memory location 0x031EF6A8. 
First-chance exception at 0x76DA4B32 in amalgam-devices-server.exe: Microsoft C++ exception: devices::util::CDeviceException at memory location 0x031EF894. 
First-chance exception at 0x76DA4B32 in amalgam-devices-server.exe: Microsoft C++ exception: devices::util::CDeviceException at memory location 0x031EF894. 
Critical error detected c0000374 

且调用栈看起来是这样的:

ntdll.dll!77a6a9ff() Unknown 
[Frames below may be incorrect and/or missing, no symbols loaded for ntdll.dll] 
ntdll.dll!77a6bcf5() Unknown 
ntdll.dll!77a6ad4a() Unknown 
ntdll.dll!77a2186c() Unknown 
ntdll.dll!779e26e8() Unknown 
ntdll.dll!779e1edb() Unknown 
ntdll.dll!779e217e() Unknown 
ntdll.dll!779e2664() Unknown 
ntdll.dll!779e2664() Unknown 
msvcr110d.dll!_free_base(void * pBlock) Line 50 C 
msvcr110d.dll!_free_dbg_nolock(void * pUserData, int nBlockUse) Line 1431 C++ 
msvcr110d.dll!_free_dbg(void * pUserData, int nBlockUse) Line 1265 C++ 
msvcr110d.dll!operator delete(void * pUserData) Line 54 C++ 
amalgam-devices-communication.dll!std::allocator<char>::deallocate(char * _Ptr, unsigned int __formal) Line 586 C++ 
amalgam-devices-communication.dll!std::_Wrap_alloc<std::allocator<char> >::deallocate(char * _Ptr, unsigned int _Count) Line 888 C++ 
amalgam-devices-communication.dll!std::basic_string<char,std::char_traits<char>,std::allocator<char> >::_Tidy(bool _Built, unsigned int _Newsize) Line 2265 C++ 
amalgam-devices-communication.dll!std::basic_string<char,std::char_traits<char>,std::allocator<char> >::~basic_string<char,std::char_traits<char>,std::allocator<char> >() Line 965 C++ 
amalgam-devices-communication.dll!invoke_fn::operator()(devices::server::CDeviceServer & server, boost::promise<void> & boostPromise, CDeviceClientRequest & request, CDeviceServerResponse & response) Line 157 C++ 
> amalgam-devices-communication.dll!<lambda_e4b2ef864a7f24dec10bd7126eadbce1>::operator()() Line 185 C++ 
amalgam-devices-communication.dll!boost::detail::thread_data<<lambda_e4b2ef864a7f24dec10bd7126eadbce1> >::run() Line 75 C++ 
amalgam-devices-communication.dll!boost::`anonymous namespace'::thread_start_function(void * param) Line 191 C++ 
msvcr110d.dll!_callthreadstartex() Line 354 C 
msvcr110d.dll!_threadstartex(void * ptd) Line 337 C 
kernel32.dll!7735850d() Unknown 
ntdll.dll!779ebf39() Unknown 
ntdll.dll!779ebf0c() Unknown 

任何帮助非常感谢,我们一直在努力,现在追踪下来了好几天。

回答

1

我已经找出了问题所在。 CDeviceResponse对象通过引用 传递给boost :: thread中的仿函数。如果线程超时,主线程将接管 并且该对象超出范围并被销毁。

但是,包含服务器调用的线程不会被终止,它会继续运行,直到最终它抛出自己的异常并杀死该线程。此时, 对象再次被销毁,导致访问冲突。

有2种可能的修复方法。

  1. 捕获invokeWithTimeout()返回false并显式终止线程的事实。这将涉及到记忆有关线的详细信息,以及我们所需要的代码维护更简单,所以我们去了选项2

  2. 定义的请求和响应共享指针,并通过这些来仿函数和 调用来代替。这样,当主线程存在并继续运行时,只有参考计数减少。当实际的线程死亡时,它会成功破坏对象。

但是如果有人知道的查杀超时线程这是调用 与下面的代码更清洁的方式,那么请让我知道:

boost::promise<void>  boostPromise; 
boost::unique_future<void> boostFuture = boostPromise.get_future(); 
boost::thread    boostThread([&]() 
          { 
           invoke_fn functor; 
           functor(*this, 
             boostPromise, 
             request, 
             response); 
          }); 

bool result = (boostFuture.wait_for(boost::chrono::milliseconds(timeout)) 
       == 
       boost::future_status::ready); 
>> if result false kill the thread here 
return result;