Skip to content

binary_iarchive and binary_oarchive constructors crash in Visual Studio 2026 in Debug mode #389

Closed

Description

@jmairboeck

The following simple test program crashes when building it with Visual Studio 2026 (18.12, MSVC 14.52) in Debug mode, using the official binaries of Boost 1.92.0.

#include <string>
#include <iostream>
#include <sstream>
#include <fstream>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/archive/binary_oarchive.hpp>


auto serialize() -> std::string {
  std::ostringstream os;
  boost::archive::binary_oarchive a{ os };
  return os.str();
}

void deserialize(const std::string& data) {
  std::istringstream is { data };
  boost::archive::binary_iarchive a{ is };
}


int main()
{
  std::string str = serialize();
  //std::ifstream file{ "test.bin", std::ios::binary };
  //file >> str;
  std::cout << str << std::endl;
  std::ofstream file{ "test.bin", std::ios::binary };
  file << str;
  file.close();
  //deserialize(str);
}

Stack trace from the debugger:

 	[Ausnahme ausgelöst bei 0x00007FFF9887F1A8 (msvcp140d.dll) in ConsoleApplication1.exe: 0xC0000005: Zugriffsverletzung beim Lesen an Position 0xFFFFFFFFFFFFFFFF.]	
 	msvcp140d.dll!std::basic_streambuf<char,std::char_traits<char>>::sputn(const char * _Ptr, __int64 _Count) Zeile 171	C++
 	ConsoleApplication1.exe!boost::archive::basic_binary_oprimitive<boost::archive::binary_oarchive,char,std::char_traits<char>>::save_binary(const void * address, unsigned __int64 count) Zeile 167	C++
 	ConsoleApplication1.exe!boost::archive::basic_binary_oprimitive<boost::archive::binary_oarchive,char,std::char_traits<char>>::save<unsigned __int64>(const unsigned __int64 & t) Zeile 90	C++
 	ConsoleApplication1.exe!boost::archive::basic_binary_oprimitive<boost::archive::binary_oarchive,char,std::char_traits<char>>::save(const std::string & s) Zeile 70	C++
 	ConsoleApplication1.exe!boost::archive::save_access::save_primitive<boost::archive::binary_oarchive,std::string>(boost::archive::binary_oarchive & ar, const std::string & t) Zeile 95	C++
 	ConsoleApplication1.exe!boost::archive::detail::save_non_pointer_type<boost::archive::binary_oarchive>::save_primitive::invoke<std::string>(boost::archive::binary_oarchive & ar, const std::string & t) Zeile 239	C++
 	ConsoleApplication1.exe!boost::archive::detail::save_non_pointer_type<boost::archive::binary_oarchive>::invoke<std::string>(boost::archive::binary_oarchive & ar, const std::string & t) Zeile 317	C++
 	ConsoleApplication1.exe!boost::archive::save<boost::archive::binary_oarchive,std::string const>(boost::archive::binary_oarchive & ar, const std::string & t) Zeile 541	C++
 	ConsoleApplication1.exe!boost::archive::detail::common_oarchive<boost::archive::binary_oarchive>::save_override<std::string const>(const std::string & t) Zeile 71	C++
 	ConsoleApplication1.exe!boost::archive::basic_binary_oarchive<boost::archive::binary_oarchive>::save_override<std::string>(const std::string & t) Zeile 80	C++
 	ConsoleApplication1.exe!boost::archive::binary_oarchive_impl<boost::archive::binary_oarchive,char,std::char_traits<char>>::save_override<std::string const>(const std::string & t) Zeile 59	C++
 	ConsoleApplication1.exe!boost::archive::detail::interface_oarchive<boost::archive::binary_oarchive>::operator<<<std::string>(const std::string & t) Zeile 71	C++
 	ConsoleApplication1.exe!boost::archive::basic_binary_oarchive<boost::archive::binary_oarchive>::init() Zeile 37	C++
 	ConsoleApplication1.exe!boost::archive::binary_oarchive_impl<boost::archive::binary_oarchive,char,std::char_traits<char>>::init(unsigned int flags) Zeile 66	C++
 	ConsoleApplication1.exe!boost::archive::binary_oarchive::binary_oarchive(std::basic_ostream<char,std::char_traits<char>> & os, unsigned int flags) Zeile 46	C++
 	ConsoleApplication1.exe!serialize() Zeile 66	C++
 	ConsoleApplication1.exe!main() Zeile 78	C++
 	ConsoleApplication1.exe!invoke_main() Zeile 79	C++
 	ConsoleApplication1.exe!__scrt_common_main_seh() Zeile 288	C++
 	ConsoleApplication1.exe!__scrt_common_main() Zeile 331	C++
 	ConsoleApplication1.exe!mainCRTStartup(void * __formal) Zeile 17	C++
	kernel32.dll!BaseThreadInitThunk()	Unbekannt
 	ntdll.dll!RtlUserThreadStart()	Unbekannt

The error message is an access violation ("this" was "0xFFFFFFFFFFFFFFFF") in sputn.

In Release mode, everything works as it should.

Activity

  1. jmairboeck commented on Nov 17, 2026

    @jmairboeck
    ContributorAuthor

    The problem only happens when linking boost_serialization statically, not with the DLL.

  2. robertramey commented on Nov 17, 2026

    @robertramey
    Member

    I'm going to confess that I'm absolutely mystified by the code in this test.

    auto serialize() -> std::string {
    std::ostringstream os;
    boost::archive::binary_oarchive a{ os };
    return os.str();
    }

    Is this meant to be equivalent to the following?

    std::string serialize() {
    std::ostringstream os;
    boost::archive::binary_oarchive a{ os };
    return os.str();
    }

    It looks like the binary_archive contains a public function named "str()" whose value is returned from the function. I have no idea what that value might be or where it comes from. There is no such function in the Archve Concept. Even assuming it there were, what value would it return? What is the test expected to display. I'm lost here. I'm sure that the problem is the latest C++ syntax is now beyond me.

  3. jmairboeck commented on Nov 18, 2026

    @jmairboeck
    ContributorAuthor

    Yes, this just constructs a binary_oarchive (named a) from an ostringstream (os) and returns the resulting string without serializing anything. str() is a member function of the ostringstream. Serializing an object is not necessary for the test because the constructor already crashes when trying to write the file signature.

  4. added a commit that references this issue on Nov 18, 2026
    8a57cf6
  5. robertramey commented on Nov 18, 2026

    @robertramey
    Member

    OK - found std::ostrsream in cppreference [https://en.cppreference.com/cpp/io/ostrstream] . Not that it's important here it says deprecated in C++98 and removed in C++26.

    I'm strongly suspecting that this is not a serialization ilbrary issue but an issue with the io library. I don't have a C++ compilation system at hand. Maybe you can help me out a little here. Does the following code exhibit the same problem? If so, we could let the serialization library off the hook. Let me know. Thanks for helping me out here.

    auto serialize() -> std::string {
    std::ostringstream os;
    // boost::archive::binary_oarchive a{ os };
    return os.str();
    }

  6. gennaroprota commented on Nov 19, 2026

    @gennaroprota
    Collaborator

    Hi Robert, did you see PR #390?

  7. robertramey commented on Nov 19, 2026

    @robertramey
    Member

    I did see PR #390 but truth is I didn't really understand it. I don't see how it would relate to this issue anyway. I'm speculating that my test above will show that there is a problem with ostringstream constructor unrelated to the serialization library itself.

    Another question occurs to me as I write this. Why would placing the contents of a binary archive into a string be accepted to work? I don't know for a fact that an std::string can hold any character. I also don't see how the internal string would be useful if it's filled with binary characters. One time as a demo/test I made an archive which bypassed the whole stream interface to store/retrieve data to an in memory buffer. This would fulfill the use case here - and of course it was very, very fast - in accordance with the main advantage of binary archive.

    Food for thought. Keep up the good work!

  8. gennaroprota commented on Nov 20, 2026

    @gennaroprota
    Collaborator

    Thanks :-).

    Just to clarify one point first: the test uses std::ostringstream, from <sstream>, not the deprecated std::ostrstream, so nothing unusual is going on there. I built your variant without the archive with the reporter's compiler (MSVC 14.52, Debug) and it works fine. The stream isn't the problem.

    The problem is a bug in MSVC 14.52 itself, which Microsoft has already acknowledged and fixed for a future release: https://developercommunity.visualstudio.com/t/C-object-layout-places-empty-non-first/11175160.

    Here's what happens. Every archive has an empty base class, interface_oarchive (or interface_iarchive), which sits at the end of common_oarchive, right after basic_oarchive. When converting a pointer to such a base, MSVC 14.52 uses an offset one byte smaller than all earlier versions do. That's harmless as long as everything is compiled by the same compiler. But the reporter links statically with the official Boost 1.92.0 binaries, which were built with an earlier MSVC, while their program is built with 14.52. With static linking, some of the inline archive functions come from the library and some from the program, and the two halves disagree by one byte about where the archive is. So the constructor, while writing the archive signature, reads the archive's stream buffer pointer from the wrong bytes, and sputn() crashes. With the DLL, the program always uses its own copies of those functions, which is why the reporter only sees it with static linking.

    I reproduced the crash by linking a library built with MSVC 14.44 into a program built with 14.52, and the stack trace matches the reporter's frame for frame. It also crashes the other way round.

    PR #390 gives the two interface classes a one-byte data member under MSVC, so they're no longer empty and every compiler version computes the same offset. With it, every archive type (text, binary, XML, wide and polymorphic) works when the library and the program are built with different MSVC versions, in both directions. Without it, the same test crashes. The member can be removed once 14.52 is no longer in use.

    About strings: a std::string can hold any char value, including '\0', because it stores its length explicitly, so it can hold the contents of a binary archive without problems. An archive writing directly to a memory buffer would certainly be faster, but it wouldn't avoid this bug, which is about the archive classes, not the stream.

  9. jmairboeck commented on Nov 20, 2026

    @jmairboeck
    ContributorAuthor

    The ostringstream part of the test is actually a simplification of the test code. The original code we use in production uses a boost::iostreams::filtering_stream with zlib compression and base64 encoding, which I removed from the test for simplicity reasons. I could have used an ofstream directly to write to a file instead, but I only added writing the result to a file in main afterwards to easier test the deserialization part without invoking the crashing serialization first (see the commented parts of the test code). Originally, the test code just serialized to a string and deserialized that again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions