Access violation in ios_base::_Ios_base_dtor (x64 msvcrtd)

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Dear community,

I am having troubles with a chunk of code Im working on. The relevant pieces:

<div style="overflow:auto;background-color:white;line-height:100% ! important;font-family:courier new;font-size:11px
<table style="border-width:0px;margin:2px 0px;width:99%;border-collapse:collapse;background-color:rgb(255, 255, 255)" cellpadding=0 cellspacing=0>
<col style="font-family:courier new;font-size:11px;padding-left:10px;white-space:nowrap
<tbody>
<tr>
<td><font style="font-size:11px </font><font style="color:green // IFileStreambuf inherits from std::streambuf</font><font style="font-size:11px  </font></td>
</tr>
<tr>
<td style="background-color:rgb(247, 247, 247) IFileStreambuf::IFileStreambuf(<font style="color:blue const</font><font style="font-size:11px  std::</font><font style="color:blue string</font><font style="font-size:11px & filename) </font></td>
</tr>
<tr>
<td>{ </td>
</tr>
<tr>
<td style="background-color:rgb(247, 247, 247)     <font style="color:green // try opening a file here</font><font style="font-size:11px  </font></td>
</tr>
<tr>
<td>    <font style="color:green // if the file isnt open:</font><font style="font-size:11px  </font></td>
</tr>
<tr>
<td style="background-color:rgb(247, 247, 247)     { </td>
</tr>
<tr>
<td>        std::stringstream msg; </td>
</tr>
<tr>
<td style="background-color:rgb(247, 247, 247)         msg << <font style="color:blue "Could not open file "</font><font style="font-size:11px  << filename << </font><font style="color:blue ": "</font><font style="font-size:11px  << get_error_string(); </font></td>
</tr>
<tr>
<td>        <font style="color:blue throw</font><font style="font-size:11px  std::runtime_error(msg.str()); </font></td>
</tr>
<tr>
<td style="background-color:rgb(247, 247, 247)     } </td>
</tr>
<tr>
<td>} </td>
</tr>
<tr>
<td style="background-color:rgb(247, 247, 247)  </td>
</tr>
<tr>
<td>IFileStreambuf::~IFileStreambuf() </td>
</tr>
<tr>
<td style="background-color:rgb(247, 247, 247) { </td>
</tr>
<tr>
<td>    <font style="color:green // close the file if its still open</font><font style="font-size:11px  </font></td>
</tr>
<tr>
<td style="background-color:rgb(247, 247, 247) } </td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td style="background-color:rgb(247, 247, 247) <font style="color:green // IFileStream inherits from std::istream</font><font style="font-size:11px  </font></td>
</tr>
<tr>
<td>IFileStream::IFileStream(<font style="color:blue const</font><font style="font-size:11px  std::</font><font style="color:blue string</font><font style="font-size:11px & filename) </font></td>
</tr>
<tr>
<td style="background-color:rgb(247, 247, 247)     : std::istream(<font style="color:blue new</font><font style="font-size:11px  IFileStreambuf(filename)) </font></td>
</tr>
<tr>
<td>{ </td>
</tr>
<tr>
<td style="background-color:rgb(247, 247, 247) } </td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td style="background-color:rgb(247, 247, 247) IFileStream::~IFileStream() </td>
</tr>
<tr>
<td>{ </td>
</tr>
<tr>
<td style="background-color:rgb(247, 247, 247)     delete rdbuf(); </td>
</tr>
<tr>
<td>} </td>
</tr>
<tr>
<td style="background-color:rgb(247, 247, 247)  </td>
</tr>
</tbody>
</table>


(The project is under the GPL; if you find the above chunks inconclusive, you can view the relevant http://supertux.lethargik.org/viewvc/viewvc.cgi/trunk/supertux/src/physfs/physfs_stream.hpp?view=markup header and http://supertux.lethargik.org/viewvc/viewvc.cgi/trunk/supertux/src/physfs/physfs_stream.cpp?view=markup code files.)

If the runtime_error in the IFileStreambuf constructor is thrown, the C++ runtime crashes with an access violation in ios_base::_Ios_base_dtor. The backtrace:

<font style="font-size:10px MSVCP90D!std::ios_base::_Ios_base_dtor+0x25
MSVCP90D!std::ios_base::~ios_base+0x22
MSVCP90D!std::basic_ios<char,std::char_traits<char> >::~basic_ios<char,std::char_traits<char> >+0x22
supertux2!`IFileStream::IFileStream::`1::dtor$0+0x25
MSVCR90D!CallSettingFrame+0x20
MSVCR90D!_FrameUnwindToState+0x1be
MSVCR90D!__FrameUnwindToEmptyState+0x8e
MSVCR90D!_InternalCxxFrameHandler+0x224
MSVCR90D!_CxxFrameHandler+0xb9
ntdll!RtlpExecuteHandlerForUnwind+0xd
ntdll!RtlUnwindEx+0x23d
MSVCR90D!UnwindNestedFrames+0xff
MSVCR90D!_FrameUnwindToState+0x32c
MSVCR90D!_InternalCxxFrameHandler+0x9bf
MSVCR90D!_InternalCxxFrameHandler+0x3e3
MSVCR90D!_CxxFrameHandler+0xb9
ntdll!RtlpExecuteHandlerForException+0xd
ntdll!RtlDispatchException+0x20c
ntdll!KiUserExceptionDispatcher+0x2e
kernel32!RaiseException+0x39
MSVCR90D!CxxThrowException+0xac
supertux2!IFileStreambuf::IFileStreambuf+0x1bd [c:developmentsourcessupertuxsrcphysfsphysfs_stream.cpp @ 43]
supertux2!IFileStream::IFileStream+0x7b [c:developmentsourcessupertuxsrcphysfsphysfs_stream.cpp @ 174]
supertux2!WorldMapNS::WorldMap::setup+0x224 [c:developmentsourcessupertuxsrcworldmapworldmap.cpp @ 947]
supertux2!MainLoop::handle_screen_switch+0x1a1 [c:developmentsourcessupertuxsrcmainloop.cpp @ 254]
supertux2!MainLoop::run+0x42 [c:developmentsourcessupertuxsrcmainloop.cpp @ 271]
supertux2!SDL_main+0x820 [c:developmentsourcessupertuxsrcmain.cpp @ 604]
supertux2!main+0x173 [c:developmentsourcessdl-1.2.13srcmainwin32sdl_win32_main.c @ 246]
supertux2!__tmainCRTStartup+0x192 [f:ddvctoolscrt_bldself_64_amd64crtsrccrtexe.c @ 582]
supertux2!mainCRTStartup+0xe [f:ddvctoolscrt_bldself_64_amd64crtsrccrtexe.c @ 399]
kernel32!BaseThreadInitThunk+0xd
ntdll!RtlUserThreadStart+0x1d</font>

If the file-loading code is moved into a function, which is then called manually by the IFileStream constructor, IFileStreambuf and IFileStream self-destruct smoothly.

Is this a bug in the MS VC++ 9 debug RTL or am I doing something totally <i>verboten</i>? Is there anything in the C++ standardization documents that handles such a case?

FWIW, the code works smoothly on OS X and Linux. Im not sure about older and/or release versions of the MSVC++RTL.

Thanks for your help in advance.


View the full article
 
Back
Top