diff --git a/lib/Epub/Epub/Section.cpp b/lib/Epub/Epub/Section.cpp index 07f24bc2..ed734aa4 100644 --- a/lib/Epub/Epub/Section.cpp +++ b/lib/Epub/Epub/Section.cpp @@ -353,6 +353,9 @@ bool Section::createSectionFile(const int fontId, const float lineCompression, c return false; } + // Evict old variants for this spine to keep cache size controlled BEFORE creating the new one + evictOldVariants(); + if (!Storage.openFileForWrite("SCT", filePath, file)) { return false; } @@ -365,9 +368,6 @@ bool Section::createSectionFile(const int fontId, const float lineCompression, c std::string contentBase = (lastSlash != std::string::npos) ? localPath.substr(0, lastSlash + 1) : ""; std::string imageBasePath = getImageBasePath(propertyHash); - // Evict old variants for this spine to keep cache size controlled - evictOldVariants(); - CssParser* cssParser = nullptr; if (embeddedStyle) { cssParser = epub->getCssParser(); @@ -483,13 +483,16 @@ bool Section::createSectionFile(const int fontId, const float lineCompression, c serialization::writePod(file, lutOffset); serialization::writePod(file, anchorMapOffset); serialization::writePod(file, paragraphLutOffset); - file.close(); + file.flush(); + if (cssParser) { cssParser->clear(); } buildTocBoundaries(anchors); + file.close(); + // Cache the LUT in memory and open the file for reading so that // subsequent loadPageFromSectionFile() calls can seek directly without re-opening. if (!Storage.openFileForRead("SCT", filePath, file)) {