nbind
nbind copied to clipboard
nbind is not compatible with std::stringstream
When a stringstream is destructed, and the function is bound to JS via nbind, the destructor throws some heap free error.
Minimal error example: void SomeClass:doSomething() { auto stream = new std::stringstream(); delete stream; }
NBIND_CLASS(SomeClass) { method(doSomething); }
the code above, if run, will crash on the line "delete stream"
I am experiencing this error also. GRBG.
Are you targeting a Node.js native addon? Which platform, C++ compiler and Node.js version does this happen on? I was unable to replicate on:
- OS X 10.8.5, Apple LLVM 5.1, Node.js 7.0.0
- Linux Mint 17.1, GCC 6.2.0, Node.js 0.10.36
Does this complete, minimal code cause the issue on your machine? If not, what changes are needed to replicate it?
#include <sstream>
#include <cstdio>
class SomeClass {
public:
void doSomething() {
auto stream = new std::stringstream();
printf("OK...\n");
delete stream;
}
};
#include <nbind/nbind.h>
NBIND_CLASS(SomeClass) {
construct<>();
method(doSomething);
}
var nbind = require('nbind');
var lib = nbind.init().lib;
new lib.SomeClass().doSomething();
Can you change -O3 to -g in 2 places in node_modules/nbind/src/nbind.gypi and recompile (remove the build directory first), then run gdb node and then type run index.js in the GDB prompt (assuming index.js is the JavaScript source file name)? Could you please paste here the stack trace if it prints one?
If you're on Windows the above commands likely won't work, so can you debug the solution in Visual Studio to get a stack trace?
I'm using MSVC 14.0 (2015) and have disabled all optimization flags.
Here's the call stack on the "delete" line: ntdll.dll!RtlReportCriticalFailure() Unknown ntdll.dll!RtlpHeapHandleError() Unknown ntdll.dll!RtlpLogHeapFailure() Unknown ntdll.dll!RtlFreeHeap() Unknown node.dll!00007ffdfbdeeb0c() Unknown node.dll!00007ffdfbd6ea89() Unknown node.dll!00007ffdfbd6eba5() Unknown [External Code] SomeClass.node!SomeClass::doSomething() Line 146 C++
I did not compile nbind with node-gyp, but used cmake-js to compile it. It works fine for other pieces of code, but only seems to fail when deconstructing std::stringstream and std::istringstream
Sounds like this might be related to #73. I'll investigate.
In my class, the constructor is not empty, it takes in several parameters and does something in its body. I also tried the same method on a separate empty class with empty constructor, it still fails.
Tried using NAN to directly call "new" and "delete" on an std::istringstream, it was completely fine and did not crash on destruction. This proves that it's not a V8 or NAN problem, but a nbind-specific problem.
edit Tried the build on gcc 6.2, the problem does not exist on Linux. It is specific to MSVC
The problem also doesn't appear with Visual Studio 14.0.25420.1 and Node.js v7.2.1 on 64bit Windows 10. It sounds like a double free or similar somewhere, but difficult to find without replicating the crash first. Neither Valgrind nor PVS-Studio found anything suspicious.
I've now added a new branch with a test for this: https://github.com/charto/nbind/commit/b09f347d2898fe14ec76a6a00f6fb596d9efbffa
AppVeyor reports no problems on Node.js versions 0.12, 4.6, 6.9 and 7.0: https://ci.appveyor.com/project/jjrv/nbind/build/0.0.366
Can you do:
git clone https://github.com/charto/nbind.git
cd nbind
git checkout fix-stringstream
npm install
npm test
Does it run into the error? If not, can you submit a PR to that branch (modifying StringStream.cc) that reproduces the error for you? Hopefully AppVeyor would then also catch it. If you do get the error, what Node.js and Windows version are you using?
Using the default node-gyp in MSVC Release mode, the test code run without any errors. However, when I manually switched the build type to Debug and runtime from MD to MDd, the error occurs.
Can you run the above commands (from git clone to npm test)? I've enabled the debug configuration (/Zi and /MDd) in the fix-stringstream branch and it still won't show the error. How does the project (under test/v8/build/nbind.vcxproj) need to be changed to show the error, or does it already happen on your system?