Last active
December 16, 2015 05:19
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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