Neil Hodgson wrote:
   I don't think the current code should ever have more than one
LineLayout checked out of the cache at any time and an assertion on
this would reveal code that could fail.

This sounds good to me. I've attached a patch that tracks the number of layouts in use and adds assertions. It also sets the length of the cache array if the array shrinks or grows without being reallocated. Previously, the length was not being set which would cause entries in the upper portion of the array to be deleted whenever AllocateForLevel was called.

Thanks,

John
? bin/SciLexer.ilk
? bin/Scintilla.ilk
Index: src/Editor.cxx
===================================================================
RCS file: /home/cvs/src/ide/external/scintilla/src/Editor.cxx,v
retrieving revision 1.15
diff -u -p -r1.15 Editor.cxx
--- src/Editor.cxx      21 Nov 2005 23:14:23 -0000      1.15
+++ src/Editor.cxx      22 Nov 2005 17:22:45 -0000
@@ -188,7 +188,7 @@ void LineLayout::RestoreBracesHighlight(
 
 LineLayoutCache::LineLayoutCache() :
        level(0), length(0), size(0), cache(0),
-       allInvalidated(false), styleClock(-1) {
+       allInvalidated(false), styleClock(-1), useCount(0) {
        Allocate(0);
 }
 
@@ -197,6 +197,7 @@ LineLayoutCache::~LineLayoutCache() {
 }
 
 void LineLayoutCache::Allocate(int length_) {
+       PLATFORM_ASSERT(cache == NULL);
        allInvalidated = false;
        length = length_;
        size = length;
@@ -211,8 +212,9 @@ void LineLayoutCache::Allocate(int lengt
 }
 
 void LineLayoutCache::AllocateForLevel(int linesOnScreen, int linesInDoc) {
+       PLATFORM_ASSERT(useCount == 0);
        int lengthForLevel = 0;
-       if (level == llcCaret) {
+        if (level == llcCaret) {
                lengthForLevel = 1;
        } else if (level == llcPage) {
                lengthForLevel = linesOnScreen + 1;
@@ -221,23 +223,28 @@ void LineLayoutCache::AllocateForLevel(i
        }
        if (lengthForLevel > size) {
                Deallocate();
-       } else if (lengthForLevel < length) {
-               for (int i = lengthForLevel; i < length; i++) {
-                       delete cache[i];
-                       cache[i] = 0;
-               }
-       }
-       if (!cache) {
                Allocate(lengthForLevel);
+       } else {
+               if (lengthForLevel < length) {
+                       for (int i = lengthForLevel; i < length; i++) {
+                               delete cache[i];
+                               cache[i] = 0;
+                       }
+               }
+               length = lengthForLevel;
        }
+       PLATFORM_ASSERT(length == lengthForLevel);
+       PLATFORM_ASSERT(cache != NULL || length == 0);
 }
 
 void LineLayoutCache::Deallocate() {
+       PLATFORM_ASSERT(useCount == 0);
        for (int i = 0; i < length; i++)
                delete cache[i];
        delete []cache;
        cache = 0;
        length = 0;
+       size = 0;
 }
 
 void LineLayoutCache::Invalidate(LineLayout::validLevel validity_) {
@@ -283,6 +290,7 @@ LineLayout *LineLayoutCache::Retrieve(in
                pos = lineNumber;
        }
        if (pos >= 0) {
+               PLATFORM_ASSERT(useCount == 0);
                if (cache && (pos < length)) {
                        if (cache[pos]) {
                                if ((cache[pos]->lineNumber != lineNumber) ||
@@ -298,6 +306,7 @@ LineLayout *LineLayoutCache::Retrieve(in
                                cache[pos]->lineNumber = lineNumber;
                                cache[pos]->inCache = true;
                                ret = cache[pos];
+                               useCount++;
                        }
                }
        }
@@ -315,6 +324,9 @@ void LineLayoutCache::Dispose(LineLayout
        if (ll) {
                if (!ll->inCache) {
                        delete ll;
+               }
+               else {
+                       useCount--;
                }
        }
 }
Index: src/Editor.h
===================================================================
RCS file: /home/cvs/src/ide/external/scintilla/src/Editor.h,v
retrieving revision 1.9
diff -u -p -r1.9 Editor.h
--- src/Editor.h        22 Aug 2005 21:41:54 -0000      1.9
+++ src/Editor.h        21 Nov 2005 22:34:28 -0000
@@ -105,6 +105,7 @@ class LineLayoutCache {
        LineLayout **cache;
        bool allInvalidated;
        int styleClock;
+       int useCount;
        void Allocate(int length_);
        void AllocateForLevel(int linesOnScreen, int linesInDoc);
 public:
_______________________________________________
Scintilla-interest mailing list
[email protected]
http://mailman.lyra.org/mailman/listinfo/scintilla-interest

Reply via email to