Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FindMember fails assertion, but code works as expected in Release mode #1525

Open
Viverea opened this issue Jun 8, 2019 · 0 comments
Open

Comments

@Viverea
Copy link

Viverea commented Jun 8, 2019

I am creating a wrapper class that allows users to save/load to files without touching the rapidjson syntax.

Wrapper class:

class LoadFile
{
public:
	LoadFile& LoadObject(const std::string& name = "");

	std::string LoadString(const std::string& name) const;
private:
	LoadFile(const rapidjson::Value& fileValue);
	const rapidjson::Value& file;

	friend class FileManager;
};

Parsing and initializing the Value&:

LoadFile& FileManager::OpenLoadFile(const string& filepath)
{
	std::ifstream fileStream(filepath);
	std::string sceneString((std::istreambuf_iterator<char>(fileStream)), std::istreambuf_iterator<char>());

	if (!sceneString.compare(""))
		throw "Unable to open file for loading!";

	Document fileDocument;
	fileDocument.Parse(sceneString.c_str());

	if (!fileDocument.IsObject())
		throw "Unable to open file for loading!";

	const Value& fileValue = fileDocument.GetObject();

	LoadFile* loadFile = new LoadFile(fileValue);

	return *loadFile;
}

How I load an object:

LoadFile& LoadFile::LoadObject(const std::string& name)
{
	Value::ConstMemberIterator finder = file.FindMember(name.c_str());

	if (finder != file.MemberEnd())
	{
		if (finder->value.IsObject())
			return *(new LoadFile(finder->value));
	}

	throw "Bad";
}

json file:

{
	"GO1": { 
		"Name": "Test1", 
		"ID": 999, 
		"Enabled": true, 
		"Rotation": 0.333, 
		"Position": { 
			"X": 1, 
			"Y": 0.5
			 }
		 }
}

Use case:

FileManager::LoadFile& loadFile = FileManager::OpenLoadFile("Assets/Temp/Test.txt");
FileManager::LoadFile& go1 = loadFile.LoadObject("GO1");
std::string hewo = go1.LoadString("Name");

On Debug mode, assertion for IsObject() fails when the FindMember in LoadObject runs.
On Release mode, nothing gets thrown and I load my Name string perfectly fine.

I have checked the parse result as well, which returns non-error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant