简体   繁体   中英

My code is working in c++ exclusively but the same code will not work from within Node.js

I have a set of Node.js c++ addons. They work perfectly on their own and I can call into their functions, so everything is properly linked. When I add calls to the external libraries I need (to access a database) everything works fine:

  • Node sees and compiles the external classes
  • I get connection errors if the DB config data is wrong.

However, if the connection data is right then it will do a core dump with no explanation. I have tried the best I can to get any data from the core dump but I have nothing.

If I take the same code I am using and compile it and run exclusively as c++ it works. So I know:

  • The connection data is correct
  • The libraries work and connect to the database (I get rows back)

So my conclusion is: there is something about running in the Node.js environment that is causing this code to break.

If someone has more Node.js experience and has suggestions on what to do or what may be happening I would greatly appreciate it.

It's a wild guess since no code has been provided but is it possible that v8 code is being executed in another thread (different from the main)?

For example, some callback invoked on successful connection that makes calls to node/v8 functions (I assume those external libraries you mention start their own threads to do their work).

If that's the case, the problem is that node/v8 code can only be invoked from the main thread. Those SO threads might be useful:

As for debugging, except from examining a core, you could also run your code with gdb so you can set breakpoints at some interesting places.

$ gdb node
(gdb) run test_connection.js 

or

(gdb) start test_connection.js 
(gdb) break <some interesting place>  
(gdb) cont

GDB run and start info

Even if you don't set breakpoints, when it crashes you'll be able to examine the stack trace and see what's going on in the different threads.

Of course, as others have suggested, everything should have been built with debug symbols.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM