diff --git a/arangod/V8Server/v8-collection.cpp b/arangod/V8Server/v8-collection.cpp index 84979aaf064d..9d899eaff8eb 100644 --- a/arangod/V8Server/v8-collection.cpp +++ b/arangod/V8Server/v8-collection.cpp @@ -3045,15 +3045,28 @@ static void JS_CollectionsVocbase( } std::vector colls; + + // clean memory + std::function cleanup; // if we are a coordinator, we need to fetch the collection info from the // agency if (ServerState::instance()->isCoordinator()) { + cleanup = [&colls]() { + for (auto& it : colls) { + if (it != nullptr) { + delete it; + } + } + }; colls = GetCollectionsCluster(vocbase); } else { colls = vocbase->collections(false); } + // make sure memory is cleaned up + TRI_DEFER(cleanup()); + std::sort(colls.begin(), colls.end(), [](LogicalCollection* lhs, LogicalCollection* rhs) -> bool { return StringUtils::tolower(lhs->name()) < StringUtils::tolower(rhs->name()); }); @@ -3066,7 +3079,7 @@ static void JS_CollectionsVocbase( size_t const n = colls.size(); size_t x = 0; for (size_t i = 0; i < n; ++i) { - auto collection = colls[i]; + auto& collection = colls[i]; if (auth->isActive() && ExecContext::CURRENT != nullptr) { AuthLevel level = auth->canUseCollection(ExecContext::CURRENT->user(), @@ -3081,6 +3094,8 @@ static void JS_CollectionsVocbase( error = true; break; } + // avoid duplicate deletion + collection = nullptr; result->Set(static_cast(x++), c); } diff --git a/arangosh/Shell/V8ClientConnection.cpp b/arangosh/Shell/V8ClientConnection.cpp index 3ccd2c69142f..1c37e2175b4b 100644 --- a/arangosh/Shell/V8ClientConnection.cpp +++ b/arangosh/Shell/V8ClientConnection.cpp @@ -309,7 +309,7 @@ static v8::Handle WrapV8ClientConnection( static void ClientConnection_ConstructorCallback( v8::FunctionCallbackInfo const& args) { - v8::Isolate* isolate = args.GetIsolate(); + TRI_V8_TRY_CATCH_BEGIN(isolate); v8::HandleScope scope(isolate); v8::Local wrap = v8::Local::Cast(args.Data()); @@ -350,6 +350,7 @@ static void ClientConnection_ConstructorCallback( } TRI_V8_RETURN(WrapV8ClientConnection(isolate, v8connection.release())); + TRI_V8_TRY_CATCH_END } //////////////////////////////////////////////////////////////////////////////// @@ -504,7 +505,7 @@ static void ClientConnection_httpGetRaw( static void ClientConnection_httpHeadAny( v8::FunctionCallbackInfo const& args, bool raw) { - v8::Isolate* isolate = args.GetIsolate(); + TRI_V8_TRY_CATCH_BEGIN(isolate); v8::HandleScope scope(isolate); // get the connection @@ -530,6 +531,7 @@ static void ClientConnection_httpHeadAny( } TRI_V8_RETURN(v8connection->headData(isolate, *url, headerFields, raw)); + TRI_V8_TRY_CATCH_END } //////////////////////////////////////////////////////////////////////////////// @@ -556,7 +558,7 @@ static void ClientConnection_httpHeadRaw( static void ClientConnection_httpDeleteAny( v8::FunctionCallbackInfo const& args, bool raw) { - v8::Isolate* isolate = args.GetIsolate(); + TRI_V8_TRY_CATCH_BEGIN(isolate); v8::HandleScope scope(isolate); // get the connection @@ -588,6 +590,7 @@ static void ClientConnection_httpDeleteAny( } TRI_V8_RETURN(v8connection->deleteData(isolate, *url, headerFields, raw, std::string())); + TRI_V8_TRY_CATCH_END } //////////////////////////////////////////////////////////////////////////////// @@ -614,7 +617,7 @@ static void ClientConnection_httpDeleteRaw( static void ClientConnection_httpOptionsAny( v8::FunctionCallbackInfo const& args, bool raw) { - v8::Isolate* isolate = args.GetIsolate(); + TRI_V8_TRY_CATCH_BEGIN(isolate); v8::HandleScope scope(isolate); // get the connection @@ -643,6 +646,7 @@ static void ClientConnection_httpOptionsAny( TRI_V8_RETURN( v8connection->optionsData(isolate, *url, *body, headerFields, raw)); + TRI_V8_TRY_CATCH_END } //////////////////////////////////////////////////////////////////////////////// @@ -669,7 +673,7 @@ static void ClientConnection_httpOptionsRaw( static void ClientConnection_httpPostAny( v8::FunctionCallbackInfo const& args, bool raw) { - v8::Isolate* isolate = args.GetIsolate(); + TRI_V8_TRY_CATCH_BEGIN(isolate); v8::HandleScope scope(isolate); // get the connection @@ -698,6 +702,7 @@ static void ClientConnection_httpPostAny( TRI_V8_RETURN( v8connection->postData(isolate, *url, *body, headerFields, raw)); + TRI_V8_TRY_CATCH_END } //////////////////////////////////////////////////////////////////////////////// @@ -724,7 +729,7 @@ static void ClientConnection_httpPostRaw( static void ClientConnection_httpPutAny( v8::FunctionCallbackInfo const& args, bool raw) { - v8::Isolate* isolate = args.GetIsolate(); + TRI_V8_TRY_CATCH_BEGIN(isolate); v8::HandleScope scope(isolate); // get the connection @@ -752,6 +757,7 @@ static void ClientConnection_httpPutAny( } TRI_V8_RETURN(v8connection->putData(isolate, *url, *body, headerFields, raw)); + TRI_V8_TRY_CATCH_END } //////////////////////////////////////////////////////////////////////////////// @@ -778,7 +784,7 @@ static void ClientConnection_httpPutRaw( static void ClientConnection_httpPatchAny( v8::FunctionCallbackInfo const& args, bool raw) { - v8::Isolate* isolate = args.GetIsolate(); + TRI_V8_TRY_CATCH_BEGIN(isolate); v8::HandleScope scope(isolate); // get the connection @@ -807,6 +813,7 @@ static void ClientConnection_httpPatchAny( TRI_V8_RETURN( v8connection->patchData(isolate, *url, *body, headerFields, raw)); + TRI_V8_TRY_CATCH_END } //////////////////////////////////////////////////////////////////////////////// @@ -833,7 +840,7 @@ static void ClientConnection_httpPatchRaw( static void ClientConnection_httpSendFile( v8::FunctionCallbackInfo const& args) { - v8::Isolate* isolate = args.GetIsolate(); + TRI_V8_TRY_CATCH_BEGIN(isolate); v8::HandleScope scope(isolate); // get the connection @@ -879,6 +886,7 @@ static void ClientConnection_httpSendFile( } TRI_V8_RETURN(result); + TRI_V8_TRY_CATCH_END } //////////////////////////////////////////////////////////////////////////////// @@ -887,7 +895,7 @@ static void ClientConnection_httpSendFile( static void ClientConnection_getEndpoint( v8::FunctionCallbackInfo const& args) { - v8::Isolate* isolate = args.GetIsolate(); + TRI_V8_TRY_CATCH_BEGIN(isolate) v8::HandleScope scope(isolate); // get the connection @@ -907,6 +915,7 @@ static void ClientConnection_getEndpoint( } TRI_V8_RETURN_STD_STRING(client->endpoint()); + TRI_V8_TRY_CATCH_END } //////////////////////////////////////////////////////////////////////////////// diff --git a/arangosh/Shell/V8ShellFeature.cpp b/arangosh/Shell/V8ShellFeature.cpp index 6790901c896d..0d6d5990a5a1 100644 --- a/arangosh/Shell/V8ShellFeature.cpp +++ b/arangosh/Shell/V8ShellFeature.cpp @@ -160,8 +160,13 @@ void V8ShellFeature::unprepare() { v8::Context::Scope context_scope{context}; - // remove any objects stored in _last global value - context->Global()->Delete(TRI_V8_ASCII_STRING2(_isolate, "_last")); + // clear globals to free memory + auto globals = _isolate->GetCurrentContext()->Global(); + v8::Handle names = globals->GetOwnPropertyNames(); + uint32_t const n = names->Length(); + for (uint32_t i = 0; i < n; ++i) { + globals->Delete(names->Get(i)); + } TRI_RunGarbageCollectionV8(_isolate, 2500.0); } @@ -178,7 +183,7 @@ void V8ShellFeature::unprepare() { _context.Reset(); } - + _isolate->Dispose(); // turn on memory allocation failures again @@ -304,8 +309,8 @@ V8ClientConnection* V8ShellFeature::setup( int V8ShellFeature::runShell(std::vector const& positionals) { v8::Locker locker{_isolate}; - v8::Isolate::Scope isolate_scope(_isolate); + v8::HandleScope handle_scope(_isolate); v8::Local context = @@ -434,7 +439,7 @@ int V8ShellFeature::runShell(std::vector const& positionals) { V8PlatformFeature::resetOutOfMemory(_isolate); } } - + if (!_console->quiet()) { _console->printLine(""); _console->printByeBye();