GC and sensible data read by File

frame frame86 at live.com
Sun Oct 4 12:10:39 UTC 2020


Hello,

I'm new to D and try to find out a memory leak in my program. I 
inspected the private bytes with VmMap on Windows to see which 
data is still kept.

Besides the actual memory leak I was surprised to find out there 
are contents of my previoulsy read configuration INI file.

So my question is: Is this expected behaviour? Why does the GC 
then not overwrite the contents with 0? Or did I something wrong 
in my function or does File not clear the data afterwards? And if 
so, how can I read sensible data from a file with standard 
methods which should not kept in memory?

I also tried manually GC.free() on buf and other places but still 
same result.

Basically this (removed some encforce() statements) and do not 
show Converter method here because the string in memory is raw 
string like a INI-comment which is never passed to my converter, 
so it's not relevant and cannot cause the problem.

Variant[string][string] parse() {


         size_t lineNr;
         char[] buf;

         string sectionName;
         string keyName;
         Variant[string][string] sections;

         auto fp = File(_fileName);
         while (!fp.eof()) {
             buf.length = 0;
             fp.readln(buf);

             string line = to!string(buf).trim();

             ++lineNr;

             // filter out empty lines
             if (line.length == 0)
                 continue;

             // filter out comments
             if (line.startsWith(";"))
                 continue;

             if (line.startsWith("[")) {
                 // section start / previous section complete
                 sectionName = line.trim("[]");
                 continue;
             }

             // regular entry
             Variant v;
             ptrdiff_t sepPos = line.indexOf("=");

             keyName = to!string(line[0 .. sepPos]).trim();
             bool isArrayEntry = keyName.indexOf("[]") > -1 && 
keyName.endsWith("]");

             string tmpVal = to!string(line[sepPos + 1 .. 
$]).trim();

             // trim text delimiter chars, if any
             tmpVal = tmpVal.trim("'\"");

             // if array, just append to existing value or create 
it
             if (isArrayEntry) {
                 keyName = keyName.trim("[]");

                 // array is always considered as string
                 // other values makes no sense in this syntax
                 string oldVal;

                 if (auto sectionPtr = sectionName in sections) {
                     if (auto keyPtr = keyName in *sectionPtr) {
                         oldVal = (*keyPtr).get!(string);
                     }
                 }

                 v = Variant(oldVal ~ tmpVal);
             }
             else {
                 v = Converter.autoConvertFromString(tmpVal);
             }

             sections[sectionName][keyName] = v;
         }

         return sections;
     }



More information about the Digitalmars-d-learn mailing list