Skip to content

Commit

Permalink
LruCache, bugfix: memory leak.
Browse files Browse the repository at this point in the history
  • Loading branch information
xicilion committed Oct 11, 2019
1 parent b790783 commit f6ba544
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 23 deletions.
12 changes: 6 additions & 6 deletions fibjs/include/LruCache.h
Expand Up @@ -191,14 +191,14 @@ class LruCache : public LruCache_base {

void expire(std::map<exlib::string, _linkedNode>::iterator it)
{
obj_ptr<EventInfo> ei = new EventInfo(this, "expire");
ei->put("key", it->first);
ei->put("value", GetPrivate(it->first));
if (m_has_event) {
obj_ptr<EventInfo> ei = new EventInfo(this, "expire");
ei->put("key", it->first);
ei->put("value", GetPrivate(it->first));

Variant v = ei;

if(m_has_event)
Variant v = ei;
_emit("expire", &v, 1);
}

remove(it);
}
Expand Down
36 changes: 19 additions & 17 deletions fibjs/include/object.h
Expand Up @@ -283,37 +283,39 @@ class object_base : public obj_base {
static void __new(const T& args) {}

public:
v8::Local<v8::Value> GetPrivate(exlib::string key)
v8::Local<v8::Object> GetPrivateObject()
{
v8::Local<v8::Object> o = wrap();
Isolate* isolate = holder();

v8::MaybeLocal<v8::Value> mv = o->GetPrivate(o->CreationContext(),
v8::Private::ForApi(isolate->m_isolate, isolate->NewString(key)));
v8::Local<v8::Private> k = v8::Private::ForApi(isolate->m_isolate, isolate->NewString("_private_object"));
v8::MaybeLocal<v8::Value> mv = o->GetPrivate(o->CreationContext(), k);

if (!mv.IsEmpty()) {
v8::Local<v8::Value> v = mv.ToLocalChecked();
if(v->IsObject())
return v8::Local<v8::Object>::Cast(v);
}

if (mv.IsEmpty())
return v8::Undefined(isolate->m_isolate);
v8::Local<v8::Object> po = v8::Object::New(isolate->m_isolate);
o->SetPrivate(o->CreationContext(), k, po);

return mv.ToLocalChecked();
return po;
}

void SetPrivate(exlib::string key, v8::Local<v8::Value> value)
v8::Local<v8::Value> GetPrivate(exlib::string key)
{
v8::Local<v8::Object> o = wrap();
Isolate* isolate = holder();
return GetPrivateObject()->Get(holder()->NewString(key));
}

o->SetPrivate(o->CreationContext(),
v8::Private::ForApi(isolate->m_isolate, isolate->NewString(key)),
value);
void SetPrivate(exlib::string key, v8::Local<v8::Value> value)
{
GetPrivateObject()->Set(holder()->NewString(key), value);
}

void DeletePrivate(exlib::string key)
{
v8::Local<v8::Object> o = wrap();
Isolate* isolate = holder();

o->DeletePrivate(o->CreationContext(),
v8::Private::ForApi(isolate->m_isolate, isolate->NewString(key)));
GetPrivateObject()->Delete(holder()->NewString(key));
}

public:
Expand Down

0 comments on commit f6ba544

Please sign in to comment.