On Tue, Dec 14, 2010 at 11:00 AM, Stephan Beal <[email protected]> wrote:
> On Tue, Dec 14, 2010 at 7:47 PM, Ross Kinder <[email protected]> wrote:
>>
>> Is there a way to do this directly that I'm missing?
>>
>> How have others handled this situation?
> Hi!
> The approach i've taken to this is basically the same as yours. The code is:
> http://code.google.com/p/v8-juice/source/browse/trunk/src/include/v8/juice/ClassWrap.h?r=1220#1433
> i think the one thing i do which you don't mention is the call of
> ClearWeak() on the Persistent Handle.
> Maybe that helps?

I'm not sure that is actually my current problem, but I can see how it
would be used.

My primary concern is providing a way to isolate unittests from each
other.  Here is the (somewhat hackish) solution I came up with, for
feedback/posterity.  This appears to solve my problem by causing my
broken dtors to crash in the current test case, rather than a few
tests later when GC finally runs.  I call
ForTestingEnsureGarbageCollected() in the tear down of each test case:

void OnGarbageCollected(v8::Persistent<v8::Value> value, void * parameter) {
  *reinterpret_cast<bool *>(parameter) = true;
  value.Dispose();
}

// static
void ForTestingEnsureGarbageCollected() {
  static bool gc_has_run = false;

  v8::Context::Scope scope(v8::Context::New());
  v8::HandleScope handle_scope;

  // Create a dummy object that will be destroyed for sure when the GC runs
  v8::Persistent<v8::Object> weakref;
  {
    v8::HandleScope handle_scope;
    v8::Handle<v8::Object> dummy_ob(v8::Object::New());
    weakref = v8::Persistent<v8::Object>::New(dummy_ob);
    gc_has_run = false;
    weakref.MakeWeak(&gc_has_run, &OnGarbageCollected);
  }

  static const int kMemoryIncrement = 0x10000;
  int adjustment = 0;
  while (!gc_has_run) {
    // via http://create.tpsitulsa.com/wiki/V8/Garbage_Collection
    v8::V8::AdjustAmountOfExternalAllocatedMemory(kMemoryIncrement);
    adjustment += kMemoryIncrement;

    v8::HandleScope handle_scope;
    v8::Handle<v8::Object> dummy_ob(v8::Object::New());
  }
  v8::V8::AdjustAmountOfExternalAllocatedMemory(adjustment * -1);
}

-- 
v8-users mailing list
[email protected]
http://groups.google.com/group/v8-users

Reply via email to