From 61dc96f9aa2512807b62cfaec35b1cd012459a6f Mon Sep 17 00:00:00 2001 From: jinming_wang Date: Thu, 28 Jan 2016 14:39:56 +0800 Subject: Fix memory leakage on Linux - part3 BUG=pdfium:348 R=jochen@chromium.org, tsepez@chromium.org Review URL: https://codereview.chromium.org/1633083002 . --- fpdfsdk/src/jsapi/fxjs_v8.cpp | 43 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 40 insertions(+), 3 deletions(-) (limited to 'fpdfsdk/src/jsapi') diff --git a/fpdfsdk/src/jsapi/fxjs_v8.cpp b/fpdfsdk/src/jsapi/fxjs_v8.cpp index b06f747edb..9b6b5fa445 100644 --- a/fpdfsdk/src/jsapi/fxjs_v8.cpp +++ b/fpdfsdk/src/jsapi/fxjs_v8.cpp @@ -125,6 +125,31 @@ void FXJS_ArrayBufferAllocator::Free(void* data, size_t length) { free(data); } +void V8TemplateMapTraits::Dispose(v8::Isolate* isolate, + v8::Global value, + void* key) { + v8::Local obj = value.Get(isolate); + if (obj.IsEmpty()) + return; + int id = FXJS_GetObjDefnID(obj); + if (id == -1) + return; + + CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(isolate, id); + if (!pObjDef) + return; + if (pObjDef->m_pDestructor) + pObjDef->m_pDestructor(obj); + FXJS_FreePrivate(obj); +} + +V8TemplateMapTraits::MapType* V8TemplateMapTraits::MapFromWeakCallbackInfo( + const v8::WeakCallbackInfo& data) { + V8TemplateMap* pMap = + (FXJS_PerIsolateData::Get(data.GetIsolate()))->m_pDynamicObjsMap; + return pMap ? &pMap->m_map : nullptr; +} + void FXJS_Initialize(unsigned int embedderDataSlot, v8::Isolate* pIsolate) { if (g_isolate) { ASSERT(g_embedderDataSlot == embedderDataSlot); @@ -290,6 +315,10 @@ void FXJS_InitializeRuntime( v8::Context::Scope context_scope(v8Context); FXJS_PerIsolateData::SetUp(pIsolate); + FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(pIsolate); + if (!pData) + return; + pData->CreateDynamicObjsMap(pIsolate); v8Context->SetAlignedPointerInEmbedderData(kPerContextDataIndex, pIRuntime); int maxID = CFXJS_ObjDefinition::MaxID(pIsolate); @@ -315,7 +344,8 @@ void FXJS_InitializeRuntime( v8::NewStringType::kNormal, bs.GetLength()).ToLocalChecked(); - v8::Local obj = FXJS_NewFxDynamicObj(pIsolate, pIRuntime, i); + v8::Local obj = + FXJS_NewFxDynamicObj(pIsolate, pIRuntime, i, true); v8Context->Global()->Set(v8Context, m_ObjName, obj).FromJust(); pStaticObjects->at(i) = new v8::Global(pIsolate, obj); } @@ -338,6 +368,7 @@ void FXJS_ReleaseRuntime(v8::Isolate* pIsolate, FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(pIsolate); if (!pData) return; + pData->ReleaseDynamicObjsMap(); #ifdef PDF_ENABLE_XFA // XFA, if present, should have already cleaned itself up. @@ -417,7 +448,8 @@ int FXJS_Execute(v8::Isolate* pIsolate, v8::Local FXJS_NewFxDynamicObj(v8::Isolate* pIsolate, IJS_Runtime* pIRuntime, - int nObjDefnID) { + int nObjDefnID, + bool bStatic) { v8::Isolate::Scope isolate_scope(pIsolate); v8::Local context = pIsolate->GetCurrentContext(); if (nObjDefnID == -1) { @@ -441,10 +473,15 @@ v8::Local FXJS_NewFxDynamicObj(v8::Isolate* pIsolate, if (!pObjDef->GetInstanceTemplate()->NewInstance(context).ToLocal(&obj)) return v8::Local(); - obj->SetAlignedPointerInInternalField(0, new CFXJS_PerObjectData(nObjDefnID)); + CFXJS_PerObjectData* pPerObjData = new CFXJS_PerObjectData(nObjDefnID); + obj->SetAlignedPointerInInternalField(0, pPerObjData); if (pObjDef->m_pConstructor) pObjDef->m_pConstructor(pIRuntime, obj); + if (!bStatic && FXJS_PerIsolateData::Get(pIsolate)->m_pDynamicObjsMap) { + FXJS_PerIsolateData::Get(pIsolate) + ->m_pDynamicObjsMap->set(pPerObjData, obj); + } return obj; } -- cgit v1.2.3