I’m trying to write a test for json serialization using boost::json and gtest in MSVC, however json::serialize returns for non-empty json::value/object. The code runs fine main (serializes and parses as expected); however the when I run similar code in TEST I get an SEH exception with code 0xc0000005.
This gtest throws a runtime error: “unknown file: error: SEH exception with code 0xc0000005 thrown in the test body.”
TEST(CardTests, JsonSave) {
Card card("word", "hint", "text", "translation");
json::object json = card.to_json();//
Card test1(json); // constructs correctly
EXPECT_EQ(card, test1);//equal as expected
string s = json::serialize(json);// null string with length 142
ASSERT_NE(s.size(), 0);//passes
json::value parsed = json::parse(s);//SEH exception with code 0xc0000005 (I'm guessing something to do with a null pointer dereference
Card test2(parsed); //not reached
EXPECT_EQ(card, test2); //not reached
}
However in calling the following function in main returns true with expected message.
bool test_card_json() {
Card card("test","hint","text", "translation");
json::object json_data = card.to_json();
if (card != Card(json_data)) return false;
string s = json::serialize(json_data);
if (s.size() == 0) return false;
json::value parsed{};
try { parsed = json::parse(s);}
catch (...) { return false; }
if (card != Card(parsed.as_object())) return false;
std::cout << "Card saved and loaded successfully\n";
return true;
}
Both files have:
namespace json = boost::json;
using string = std::string;
I’m not sure if there’s something funky going on with the settings between the solutions, or if there’s memory access bug in both tests that only causes a crash in the gtest. If it matters, I am running tests in a separate project under the same solution Visual Studio 2022 solution using in debug mode on c++ 23. Running two trivial pass and fail gtests gives the expected results.
Sorry if any information is missing. I’m pretty new to programming and this is my first post on Stack Overflow.