Skip to content

Commit

Permalink
Use ConcurrentQueue due a race condition. (#311)
Browse files Browse the repository at this point in the history
  • Loading branch information
viniciusjarina committed Jul 27, 2019
1 parent be037f7 commit 8a34d2f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
10 changes: 5 additions & 5 deletions src/ObjectTranslator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.IO;
using System.Reflection;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Runtime.InteropServices;
using KeraLua;

Expand Down Expand Up @@ -50,7 +51,7 @@ public int GetHashCode(object obj)
// object # to object (FIXME - it should be possible to get object address as an object #)
readonly Dictionary<int, object> _objects = new Dictionary<int, object>();

readonly Queue<int> finalizedReferences = new Queue<int>();
readonly ConcurrentQueue<int> finalizedReferences = new ConcurrentQueue<int>();

internal EventHandlerContainer PendingEvents = new EventHandlerContainer();
MetaFunctions metaFunctions;
Expand Down Expand Up @@ -1136,11 +1137,10 @@ void CleanFinalizedReferences(LuaState state)
if (finalizedReferences.Count == 0)
return;

while (finalizedReferences.Count != 0)
{
int reference = finalizedReferences.Dequeue();
int reference;

while (finalizedReferences.TryDequeue(out reference))
state.Unref(LuaRegistry.Index, reference);
}
}
}
}
3 changes: 2 additions & 1 deletion tests/src/LuaTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2384,7 +2384,7 @@ void ImplicitlyCreateATable(Lua lua)

void PleaseRunFinalizers()
{
for (int i = 0; i < 20; i++)
for (int i = 0; i < 40; i++)
{
GC.Collect();
GC.WaitForPendingFinalizers();
Expand Down Expand Up @@ -2412,6 +2412,7 @@ public void TestTableFinalizerDispose()

ImplicitlyCreateATable(lua);

lua.State.GarbageCollector(LuaGC.Collect, 0);
lua.State.GarbageCollector(LuaGC.Collect, 0);

int after2 = lua.State.GarbageCollector(LuaGC.Count, 0);
Expand Down

0 comments on commit 8a34d2f

Please sign in to comment.