Why are commands executing out of order?

Josh moonburntm at gmail.com
Fri Feb 1 22:49:43 PST 2013


Here's my code:

import std.datetime;
import std.file;
import std.stdio;

struct info
{
     string name;
     bool isDir;
     ulong size;
     SysTime timeCreated;
     SysTime timeLastAccessed;
     SysTime timeLastModified;

     this(DirEntry d)
     {
         this.name = d.name;
         this.isDir = d.isDir;
         this.size = d.size;
         this.timeCreated = d.timeCreated;
         this.timeLastAccessed = d.timeLastAccessed;
         this.timeLastModified = d.timeLastModified;
     }
}

void main()
{
     writeln("Scanning drives...");
     info[] results;
     for (char c = 'A'; c <= 'Z'; c++)
     {
         if (exists(c ~ ":\\"))
             results ~= info(DirEntry(c ~ ":\\")) ~ scan(c ~ 
":\\");
     }
     File f = File("driveInfo.txt", "w");
     foreach (i; results)
     {
         f.writeln(i);
     }
     f.close();
     writeln(memSize(results));
}

info[] scan(string dir)
{
     info[] results;
     try
     {
         auto de = dirEntries(dir, SpanMode.shallow);
         foreach (d; de)
         {
             if (d.isDir())
                 results ~= info(d) ~ scan(d.name);
             else
                     results ~= info(d);
         }
     }
     catch (FileException fe){}
     return results;
}

size_t memSize(T)(T[] t)
{
     return t.length * T.sizeof;
}

But main's first writeln actually outputs after f.close().

The program uses ~1GB of RAM overall, even though main.results is 
~50MB according to memSize. Any attempts to call the GC do 
nothing, but I'm probably doing it wrong though. Is it possible 
that I have a memory leak somewhere that is causing this delay, 
or is this a DMD problem, or something else I haven't even 
thought of?

DMD v2.060, Windows 7 x64

Thanks,

Josh


More information about the Digitalmars-d-learn mailing list