Fix memory leak from d8 shell.

We were not disposing the semaphores and the thread used in SourceGroup.

R=mnaganov@chromium.org
Signed-off-by: 's avatarThiago Farina <tfarina@chromium.org>

Review URL: http://codereview.chromium.org/7754007/

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@9198 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent c1dc429c
...@@ -1037,7 +1037,7 @@ i::Thread::Options SourceGroup::GetThreadOptions() { ...@@ -1037,7 +1037,7 @@ i::Thread::Options SourceGroup::GetThreadOptions() {
void SourceGroup::ExecuteInThread() { void SourceGroup::ExecuteInThread() {
Isolate* isolate = Isolate::New(); Isolate* isolate = Isolate::New();
do { do {
if (next_semaphore_ != NULL) next_semaphore_->Wait(); if (!next_semaphore_.is_empty()) next_semaphore_->Wait();
{ {
Isolate::Scope iscope(isolate); Isolate::Scope iscope(isolate);
Locker lock(isolate); Locker lock(isolate);
...@@ -1049,15 +1049,15 @@ void SourceGroup::ExecuteInThread() { ...@@ -1049,15 +1049,15 @@ void SourceGroup::ExecuteInThread() {
} }
context.Dispose(); context.Dispose();
} }
if (done_semaphore_ != NULL) done_semaphore_->Signal(); if (!done_semaphore_.is_empty()) done_semaphore_->Signal();
} while (!Shell::options.last_run); } while (!Shell::options.last_run);
isolate->Dispose(); isolate->Dispose();
} }
void SourceGroup::StartExecuteInThread() { void SourceGroup::StartExecuteInThread() {
if (thread_ == NULL) { if (thread_.is_empty()) {
thread_ = new IsolateThread(this); thread_ = i::SmartPointer<i::Thread>(new IsolateThread(this));
thread_->Start(); thread_->Start();
} }
next_semaphore_->Signal(); next_semaphore_->Signal();
...@@ -1065,10 +1065,9 @@ void SourceGroup::StartExecuteInThread() { ...@@ -1065,10 +1065,9 @@ void SourceGroup::StartExecuteInThread() {
void SourceGroup::WaitForThread() { void SourceGroup::WaitForThread() {
if (thread_ == NULL) return; if (thread_.is_empty()) return;
if (Shell::options.last_run) { if (Shell::options.last_run) {
thread_->Join(); thread_->Join();
thread_ = NULL;
} else { } else {
done_semaphore_->Wait(); done_semaphore_->Wait();
} }
......
...@@ -28,11 +28,11 @@ ...@@ -28,11 +28,11 @@
#ifndef V8_D8_H_ #ifndef V8_D8_H_
#define V8_D8_H_ #define V8_D8_H_
#ifndef V8_SHARED #ifndef V8_SHARED
#include "v8.h"
#include "allocation.h" #include "allocation.h"
#include "hashmap.h" #include "hashmap.h"
#include "smart-pointer.h"
#include "v8.h"
#else #else
#include "../include/v8.h" #include "../include/v8.h"
#endif // V8_SHARED #endif // V8_SHARED
...@@ -122,11 +122,10 @@ class SourceGroup { ...@@ -122,11 +122,10 @@ class SourceGroup {
#ifndef V8_SHARED #ifndef V8_SHARED
next_semaphore_(v8::internal::OS::CreateSemaphore(0)), next_semaphore_(v8::internal::OS::CreateSemaphore(0)),
done_semaphore_(v8::internal::OS::CreateSemaphore(0)), done_semaphore_(v8::internal::OS::CreateSemaphore(0)),
thread_(NULL),
#endif // V8_SHARED #endif // V8_SHARED
argv_(NULL), argv_(NULL),
begin_offset_(0), begin_offset_(0),
end_offset_(0) { } end_offset_(0) {}
void Begin(char** argv, int offset) { void Begin(char** argv, int offset) {
argv_ = const_cast<const char**>(argv); argv_ = const_cast<const char**>(argv);
...@@ -158,9 +157,9 @@ class SourceGroup { ...@@ -158,9 +157,9 @@ class SourceGroup {
static i::Thread::Options GetThreadOptions(); static i::Thread::Options GetThreadOptions();
void ExecuteInThread(); void ExecuteInThread();
i::Semaphore* next_semaphore_; i::SmartPointer<i::Semaphore> next_semaphore_;
i::Semaphore* done_semaphore_; i::SmartPointer<i::Semaphore> done_semaphore_;
i::Thread* thread_; i::SmartPointer<i::Thread> thread_;
#endif // V8_SHARED #endif // V8_SHARED
void ExitShell(int exit_code); void ExitShell(int exit_code);
......
...@@ -37,35 +37,31 @@ namespace internal { ...@@ -37,35 +37,31 @@ namespace internal {
template<typename T> template<typename T>
class SmartPointer { class SmartPointer {
public: public:
// Default constructor. Constructs an empty scoped pointer.
inline SmartPointer() : p_(NULL) {}
// Default constructor. Construct an empty scoped pointer. // Constructs a scoped pointer from a plain one.
inline SmartPointer() : p(NULL) {} explicit inline SmartPointer(T* ptr) : p_(ptr) {}
// Construct a scoped pointer from a plain one.
explicit inline SmartPointer(T* pointer) : p(pointer) {}
// Copy constructor removes the pointer from the original to avoid double // Copy constructor removes the pointer from the original to avoid double
// freeing. // freeing.
inline SmartPointer(const SmartPointer<T>& rhs) : p(rhs.p) { inline SmartPointer(const SmartPointer<T>& rhs) : p_(rhs.p_) {
const_cast<SmartPointer<T>&>(rhs).p = NULL; const_cast<SmartPointer<T>&>(rhs).p_ = NULL;
} }
// When the destructor of the scoped pointer is executed the plain pointer // When the destructor of the scoped pointer is executed the plain pointer
// is deleted using DeleteArray. This implies that you must allocate with // is deleted using DeleteArray. This implies that you must allocate with
// NewArray. // NewArray.
inline ~SmartPointer() { if (p) DeleteArray(p); } inline ~SmartPointer() { if (p_) DeleteArray(p_); }
inline T* operator->() const { return p_; }
// You can get the underlying pointer out with the * operator. // You can get the underlying pointer out with the * operator.
inline T* operator*() { return p; } inline T* operator*() { return p_; }
// You can use [n] to index as if it was a plain pointer // You can use [n] to index as if it was a plain pointer
inline T& operator[](size_t i) { inline T& operator[](size_t i) {
return p[i]; return p_[i];
} }
// We don't have implicit conversion to a T* since that hinders migration: // We don't have implicit conversion to a T* since that hinders migration:
...@@ -77,31 +73,26 @@ class SmartPointer { ...@@ -77,31 +73,26 @@ class SmartPointer {
// deleted then call Detach(). Afterwards, the smart pointer is empty // deleted then call Detach(). Afterwards, the smart pointer is empty
// (NULL). // (NULL).
inline T* Detach() { inline T* Detach() {
T* temp = p; T* temp = p_;
p = NULL; p_ = NULL;
return temp; return temp;
} }
// Assignment requires an empty (NULL) SmartPointer as the receiver. Like // Assignment requires an empty (NULL) SmartPointer as the receiver. Like
// the copy constructor it removes the pointer in the original to avoid // the copy constructor it removes the pointer in the original to avoid
// double freeing. // double freeing.
inline SmartPointer& operator=(const SmartPointer<T>& rhs) { inline SmartPointer& operator=(const SmartPointer<T>& rhs) {
ASSERT(is_empty()); ASSERT(is_empty());
T* tmp = rhs.p; // swap to handle self-assignment T* tmp = rhs.p_; // swap to handle self-assignment
const_cast<SmartPointer<T>&>(rhs).p = NULL; const_cast<SmartPointer<T>&>(rhs).p_ = NULL;
p = tmp; p_ = tmp;
return *this; return *this;
} }
inline bool is_empty() { return p_ == NULL; }
inline bool is_empty() {
return p == NULL;
}
private: private:
T* p; T* p_;
}; };
} } // namespace v8::internal } } // namespace v8::internal
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment