I am myself fairly new to V8, but your handles are dependent on your HandleScope, so when scriptCompile finishes your handles get out of scope and will be cleaned up by the garbage collector.
To ensure the lifetime of your handles you could use Persistent<> handles instead. On Friday, 24 January 2014 21:23:06 UTC+1, Pierre Saunier wrote: > > Hi, > > I want to use V8 as the scripting engine of my 3D modeler... but for the > moment, I have some troubles... > > basicaly, I have a structre like this: > typedef struct > { > v8::Isolate* isolate; > v8::Handle<v8::ObjectTemplate> global; > v8::Handle<v8::Context> context; > v8::Handle<v8::Script> script; > }v8ScriptEngineDef; > > > a function to compile a script: > int scriptCompile(char *fileName, char *code, v8ScriptEngineDef *v8Engine, > scriptExternalFuncDef *externalFunc) > { > > v8::HandleScope handle_scope(v8Engine->isolate); > v8Engine->global = v8::ObjectTemplate::New(); > v8_create_file_obj(v8Engine->isolate, v8Engine->global); > v8_create_color_obj(v8Engine->isolate, v8Engine->global); > v8_create_image_obj(v8Engine->isolate, v8Engine->global); > v8_create_gui_obj(v8Engine->isolate, v8Engine->global); > > v8Engine->global->Set(v8::String::NewFromUtf8(v8Engine->isolate, > "console"), > v8::FunctionTemplate::New(v8Engine->isolate, > v8_console)); > > v8Engine->context = v8::Context::New(v8Engine->isolate, NULL, > v8Engine->global); > if(v8Engine->context.IsEmpty()) { > scriptEngineAddToConsole(v8Engine, "Error creating context\n"); > return FALSE; > } > v8Engine->context->Enter(); > > v8::TryCatch try_catch; > v8::Handle<v8::String> v8fileName = > v8::String::NewFromUtf8(v8Engine->isolate, > fileName); > v8::Handle<v8::String> v8code = > v8::String::NewFromUtf8(v8Engine->isolate, code); > v8Engine->script = v8::Script::Compile(v8code, v8fileName); > if(v8Engine->script.IsEmpty()) > { > // Print errors that happened during compilation. > ReportException(v8Engine, v8Engine->isolate, &try_catch); > return false; > } > v8Engine->context->Exit(); > // scriptRun(engine); > > > return TRUE; > } > > and a running function: > int scriptRun(v8ScriptEngineDef *v8Engine) > { > v8::HandleScope handle_scope(v8Engine->isolate); > v8::TryCatch try_catch; > v8Engine->context->Enter(); > v8::Handle<v8::Value> result = v8Engine->script->Run(); > if (result.IsEmpty()) > { > assert(try_catch.HasCaught()); > // Print errors that happened during execution. > ReportException(v8Engine, v8Engine->isolate, &try_catch); > return false; > } > assert(!try_catch.HasCaught()); > if (!result->IsUndefined()) > { > // If all went well and the result wasn't undefined then print > // the returned value. > v8::String::Utf8Value str(result); > const char* cstr = *str; > scriptEngineAddToConsole(v8Engine, (char *)cstr); > } > v8Engine->context->Exit(); > return true; > } > > The problem is if I call the running function from the Compile function > (in comment), everythink is working fine. > But, If I call the compile fonction, then the run function... crash!!! > > > Can somebody explain me what I am doing wrong? > > > thanks, > > Pierre > -- -- v8-users mailing list v8-users@googlegroups.com http://groups.google.com/group/v8-users --- You received this message because you are subscribed to the Google Groups "v8-users" group. To unsubscribe from this group and stop receiving emails from it, send an email to v8-users+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.