Skip to content

Instantly share code, notes, and snippets.

@bodokaiser
Last active December 16, 2015 05:19
Show Gist options
  • Save bodokaiser/5383405 to your computer and use it in GitHub Desktop.
Save bodokaiser/5383405 to your computer and use it in GitHub Desktop.
Example which shows C allocated memory we set on a buffer and we know how it is getting freed.
#include <v8.h>
#include <node.h>
#include <node_buffer.h>
using namespace v8;
typedef unsigned char byte;
Handle<Value> ReturnBufferWithCAllocatedMemory(const Arguments &args) {
HandleScope scope;
byte* bytes = (byte*) malloc(1000);
byte[3] = 0x04;
byte[9] = 0x76;
// would call error because buffer looses memory
// free(bytes);
return scope.Close(node::Buffer::New((char*) bytes, 1000)->handle_);
};
void init(Handle<Object> exports) {
exports->Set(String::New("returnBuffer"),
FunctionTemplate::New(ReturnBufferWithCAllocatedMemory)->GetFunction());
};
NODE_MODULE(c_allocated_buffer, init)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment