charto / nbind

:sparkles: Magical headers that make your C++ library accessible from JavaScript :rocket:
MIT License
1.98k stars 119 forks source link

nbind do not support C++ reference #69

Closed fanjiangwei7 closed 7 years ago

fanjiangwei7 commented 7 years ago

since a lot of my C++ code class input C++ reference as the constructor function param. but when I call the function in JS it it going wrong all the time. does the nbind can not support reference param. how can I do with this situation. my class like this: ChatConfigs::ChatConfigs(const std::string &resourcePath, const std::string &workPath, unsigned int deviceId); and call the nbind like this. NBIND_CLASS(ChatConfigs) { construct<const std::string &, const std::string &, unsigned int>(); }

am I has to create another constructor to walk around this problem?

hurry07 commented 7 years ago

yes, but the refered object you return must be declear with NBIND_CLASS, and this class's instances can not be created outside c++ code.

for example: class A { ... } class B { static A createNewA(...); // without the keyword "static" is OK doSthWithA(A inst); }

NBIND_CLASS(A) { } NBIND_CLASS(B) { method(createNewA); method(doSthWithA); }

in js: let insB = new B(); let refA = B.createNewA();

insB.doSthWithA(refA);

My problem is that, you will get memory leak bug when you call let insB = new B(); let refA = B.createNewA(); thousand of times.

jjrv commented 7 years ago

Sorry about the delay.

The problem was probably only with references to strings. They should now be fixed in the develop branch. Can you confirm it works for you? First remove node_modules/nbind and then run:

cd node_modules
git clone https://github.com/charto/nbind.git
cd nbind
git checkout develop
npm install
jjrv commented 7 years ago

Closing, this should be fixed in the latest release. Feel free to re-open if there's still issues.

iasna commented 6 years ago

I have version 0.3.14, still unable to use with string references any idea if its fixed in the release on not ?