[Kst] branches/work/kst/portto4/kst/src/datasources/ascii

Peter Kümmel syntheticpp at gmx.net
Mon Oct 8 14:11:47 UTC 2012


SVN commit 1319722 by kuemmel:

QVarLengthArray never frees allocated data

therefore we must enforce it by deleting the object.

 M  +16 -14    asciisource.cpp  
 M  +4 -2      asciisource.h  


--- branches/work/kst/portto4/kst/src/datasources/ascii/asciisource.cpp #1319721:1319722
@@ -187,7 +187,7 @@
 //-------------------------------------------------------------------------------------------
 AsciiSource::AsciiSource(Kst::ObjectStore *store, QSettings *cfg, const QString& filename, const QString& type, const QDomElement& e) :
   Kst::DataSource(store, cfg, filename, type),  
-  _tmpBuffer(),
+  _fileBuffer(new FileBuffer),
   _bufferedS(-10),
   _bufferedN(-10),
   _rowIndex(),
@@ -228,10 +228,7 @@
 void AsciiSource::reset() 
 {
   // forget about cached data
-  _bufferedN = -10;
-  _bufferedS = -10;
-
-  _tmpBuffer.clear();
+  clearFileBuffer();
   _rowIndex.clear();
 
   _valid = false;
@@ -338,8 +335,7 @@
   MeasureTime t("AsciiSource::internalDataSourceUpdate: " + _filename);
 
   // forget about cached data
-  _bufferedN = -10;
-  _bufferedS = -10;
+  clearFileBuffer();
 
   if (!_haveHeader) {
     _haveHeader = initRowIndex();
@@ -487,7 +483,12 @@
 //-------------------------------------------------------------------------------------------
 void AsciiSource::clearFileBuffer()
 {
-  _tmpBuffer.clear();
+  // force deletion of internal allocated memory if any
+  const int memoryOnStack = sizeof(FileBuffer) - sizeof(QVarLengthArray<char, 0>);
+  if (_fileBuffer->capacity() > memoryOnStack) {
+    delete _fileBuffer;
+    _fileBuffer = new FileBuffer;
+  }
   _bufferedS = -10;
   _bufferedN = -10;
 }
@@ -507,11 +508,12 @@
   // find a smaller allocatable size
   clearFileBuffer();
   int realloc_size = n / 2;
-  _tmpBuffer.resize(realloc_size);
-  while (_tmpBuffer.size() != realloc_size && realloc_size > 0) {
+  _fileBuffer->resize(realloc_size);
+  while (_fileBuffer->size() != realloc_size && realloc_size > 0) {
       realloc_size /= 2;
-      _tmpBuffer.resize(realloc_size);
+      _fileBuffer->resize(realloc_size);
   }
+  clearFileBuffer();
   realloc_size /= 2; // while reading available memory could shrink, just be sure
   if (realloc_size == 0) {
     QMessageBox::warning(0, "Error while reading ascii file", "File could not be read because not enough memory is available.");
@@ -573,7 +575,7 @@
     _lineending = detectLineEndingType(file);
 
 
-    bufread = readFromFile(file, _tmpBuffer, bufstart, bufread);
+    bufread = readFromFile(file, *_fileBuffer, bufstart, bufread);
     if (bufread == 0) {
       re_alloc = false;
       return 0;
@@ -583,9 +585,9 @@
   }
 
 #ifdef KST_DONT_CHECK_INDEX_IN_DEBUG
-  const char* buffer = _tmpBuffer.constData();
+  const char* buffer = _fileBuffer->constData();
 #else
-  const QVarLengthArray<char, KST_PREALLOC>& buffer = _tmpBuffer;
+  const QVarLengthArray<char, KST_PREALLOC>& buffer = *_fileBuffer;
 #endif
 
   if (_config._columnType == AsciiSourceConfig::Fixed) {
--- branches/work/kst/portto4/kst/src/datasources/ascii/asciisource.h #1319721:1319722
@@ -85,10 +85,12 @@
 #else
 #define KST_PREALLOC 1 * 1024 * 1024
 #endif
-    QVarLengthArray<char, KST_PREALLOC> _tmpBuffer;
+    typedef QVarLengthArray<char, KST_PREALLOC> FileBuffer;
+    FileBuffer* _fileBuffer;
+    void clearFileBuffer();
     int _bufferedS;
     int _bufferedN;
-    void clearFileBuffer();
+    
     QVarLengthArray<int, KST_PREALLOC> _rowIndex;
 
     friend class ConfigWidgetAscii;


More information about the Kst mailing list