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

Heap corruption when using ifstream / stringstring (ios_base) #682

Closed
bromix opened this issue Mar 11, 2020 · 2 comments
Closed

Heap corruption when using ifstream / stringstring (ios_base) #682

bromix opened this issue Mar 11, 2020 · 2 comments

Comments

@bromix
Copy link

bromix commented Mar 11, 2020

I encountered a strange problem while using ifstream or stringstream. I'm implementing a function to read settings for an application from an ini file and return the settings as an object.
But as soon as the stringstream is declared, I get a heap corruption when leaving the scope of the function. Has this to do with threading and if so, how can I secure this? The documentation is not clear for me how to accomplish this. Below is a simplified implementation of my problem.

I'm working with Node 13.10.1

#include <sstream>

Napi::Value load_from_ini_string(const Napi::CallbackInfo& info)
{
	const Napi::Env env = info.Env();
	if(info.Length() != 1)
	{
		Napi::TypeError::New(env, "Wrong number of arguments").ThrowAsJavaScriptException();
		return env.Null();
	}

	if (!info[0].IsString())
	{
		Napi::TypeError::New(env, "Wrong arguments").ThrowAsJavaScriptException();
		return env.Null();
	}

	auto ini_content = info[0].As<Napi::String>().Utf8Value();

	std::stringstream ss;  // <- this is the problem
	// as soon as this line exists and we leave the scope of this function,
	// we get a heap corruption.
	
	return Napi::String::New(env, "Hello");
}

void init(Napi::Env env, Napi::Object exports)
{
	exports.Set("load_from_ini_string", Napi::Function::New(env, load_from_ini_string));
}
@bromix
Copy link
Author

bromix commented Mar 12, 2020

Found the problem after a day of testing... cmake-js may miss some settings under Windows.
cmake-js/issues/208

@bromix bromix closed this as completed Mar 12, 2020
@begmec
Copy link

begmec commented Jan 12, 2022

I encountered the same issue but without using cmake-js. Actually it is quiet strange and in my opinion the issue isn't fixed.

As far as I know, the node.js plugin API relies on a pure C-API. That means, it shouldn't matter if I generate my node plugin code with \MD or \MT. As long as I compile node.js with VS 2015, this statement is true. But when I compile node.js with VS 2017 and above, you will get a heap corruption if you try to build your plugin with \MD. \MT instead works always fine. To me, it seems that it is whether a bug in node.js or problem with msvc.

Our node.js plugin relies on a big c++ code base where everything is compiled with \MD. Therefore, it would be great if we could compile our plugin using \MD. Maybe somebody from Node.js or @bromix has some insights or ideas?

The build that breaks the \MD compatibility is found at git hash e783475412a315d28dde17d23492c867bf546dc2
It is the switch from VS 2015 to VS 2017.

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

2 participants