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

IntArray in C++14 #15

Open
jloveric opened this issue Feb 18, 2017 · 1 comment
Open

IntArray in C++14 #15

jloveric opened this issue Feb 18, 2017 · 1 comment

Comments

@jloveric
Copy link

IntArray is being set to null when passed as argument through ffi to c functionr.

The IntArray is being initialized just fine, in node, but when passed to my c functions, it is setting the pointer to NULL. I'm using c++14 and suspect this may have something to do with the problem. Alas, it seems inconsistent, it is working in some cases and not in others -- could be memory/pointer issues in my code, but valgrind is not indicating anything in this or any other test. I'm using GCC 5.2 and node 7.5

in node

let IntArray = ArrayType(ref.types.int);
let neuronsPerLayer = new IntArray(3)
let data =spike.completeNetwork(neuronsPerLayer, 3)

in .h file

NetworkDataArray* completeNetwork(int* neuronsPerLayer, int numLayers);

in .cpp file

NetworkDataArray* completeNetwork(int* neuronsPerLayer, int numLayers) {
  //neuronsPerLayer is 0, so it crashes as soon as I try an access it
  std::vector<int> n(neuronsPerLayer, neuronsPerLayer + numLayers);
  ...
}

In the ffi file

let libSpike = ffi.Library('source/libspike.so', {
  'completeNetwork' : [NetworkDataRef,[IntArray, 'int']]})

backtrace from gcc

#0 0x00007ffff4504829 in completeNetwork (neuronsPerLayer=0x0, numLayers=3) at /home/john/SPIKE/source/spike_wrap.cpp:44
#1 0x00007ffff472ebc2 in ffi_call_unix64 () from /home/john/SPIKE/node_modules/ffi/build/Release/ffi_bindings.node
#2 0x00007ffff472d71e in ffi_call () from /home/john/SPIKE/node_modules/ffi/build/Release/ffi_bindings.node
#3 0x00007ffff4724c94 in FFI::FFICall(Nan::FunctionCallbackInfov8::Value const&) () from /home/john/SPIKE/node_modules/ffi/build/Release/ffi_bindings.node
#4 0x00007ffff4724686 in Nan::imp::FunctionCallbackWrapper(v8::FunctionCallbackInfov8::Value const&) () from /home/john/SPIKE/node_modules/ffi/build/Release/ffi_bindings.node
#5 0x0000000000a980d5 in v8::internal::FunctionCallbackArguments::Call(void ()(v8::FunctionCallbackInfov8::Value const&)) ()
#6 0x0000000000b0afec in v8::internal::MaybeHandlev8::internal::Object v8::internal::(anonymous namespace)::HandleApiCallHelper(v8::internal::Isolate
, v8::internal::Handlev8::internal::HeapObject, v8::internal::Handlev8::internal::HeapObject, v8::internal::Handlev8::internal::FunctionTemplateInfo, v8::internal::Handlev8::internal::Object, v8::internal::BuiltinArguments) ()
#7 0x0000000000b0b565 in v8::internal::Builtin_HandleApiCall(int, v8::internal::Object**, v8::internal::Isolate*) ()

Anyway, not totally sure this is IntArray problem or a problem in my own code. However, I'm not having any issues running in pure c/c++.

@vsenko
Copy link

vsenko commented Aug 17, 2018

I bet that it's GC that cleans up the memory you allocated in JS:

  1. memory is allocated in JS (let neuronsPerLayer = new IntArray(3));
  2. pointer is sent out of JS (let data =spike.completeNetwork(neuronsPerLayer, 3)) and no references to the data are left in JS;
  3. GC does his job;
  4. when C++ library tries to access the pointer, it's already reused for something else by JS.

You could try to preserve the reference to neuronsPerLayer somehow. A very primitive example:

let IntArray = ArrayType(ref.types.int);
let neuronsPerLayer = new IntArray(3);
let data =spike.completeNetwork(neuronsPerLayer, 3);
console.log(neuronsPerLayer);

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