Thread: Avisynth 2.6 MT
View Single Post
Old 13th August 2009, 12:22   #13  |  Link
SEt
Registered User
 
Join Date: Aug 2007
Posts: 374
Fixed minor threading bug and major memory leak:
Code:
--- avisynth.cpp	2009-08-08 22:20:34.550375000 +0400
+++ avisynth.cpp	2009-08-13 15:12:07.305375000 +0400
@@ -144,8 +144,9 @@
       return &i->vf;
   LinkedVideoFrame* result = (LinkedVideoFrame*)::operator new(sizeof(LinkedVideoFrame));
   result->vf.refcount=1;
-  result->next = g_Bin->g_VideoFrame_recycle_bin;
-  result->next = (LinkedVideoFrame*)InterlockedExchangePointer((long*)&g_Bin->g_VideoFrame_recycle_bin,result);//g_VideoFrame_recycle_bin is assigned twice to result->next just in case another thread reads result->next before the last assignment 
+  do
+		result->next = g_Bin->g_VideoFrame_recycle_bin;
+  while (InterlockedCompareExchangePointer((volatile PVOID*)&g_Bin->g_VideoFrame_recycle_bin, result, result->next) != result->next);
   return &result->vf;
 }
 
@@ -1639,7 +1640,7 @@
           InterlockedIncrement(&i->sequence_number);  // Signal to the cache that the vfb has been stolen
           return i;
         }
-        if (i->GetDataSize() > size) {
+        if (c > Cache::PC_Nil && i->GetDataSize() > size) {
           // Remember the smallest VFB that is bigger than our size
           if ((j == 0) || (i->GetDataSize() < j->GetDataSize()))
           {
@@ -1659,7 +1660,7 @@
 	if (!CacheHead) break; // cache state will not change in next loop iterations
 
     // Plan C: Steal the oldest, smallest free buffer that is greater in size
-    if (j && c > Cache::PC_Nil) {
+    if (j) {
       ++g_Mem_stats.PlanC;
       InterlockedIncrement(&j->sequence_number);  // Signal to the cache that the vfb has been stolen
       return j;
SEt is offline   Reply With Quote