[Issue 5059] New: String assignment in foreach loop breaks immutability

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri Oct 15 12:04:35 PDT 2010


http://d.puremagic.com/issues/show_bug.cgi?id=5059

           Summary: String assignment in foreach loop breaks immutability
           Product: D
           Version: D2
          Platform: Other
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody at puremagic.com
        ReportedBy: andrej.mitrovich at gmail.com


--- Comment #0 from Andrej Mitrovic <andrej.mitrovich at gmail.com> 2010-10-15 12:04:00 PDT ---
Since this example uses the Registry, you need Windows to compile it:

module mymodule;

import std.stdio : writeln, write;
import std.windows.registry;

void main()
{
   Key HKLM = Registry.localMachine;
   Key SFW = HKLM.getKey("software");

   string[] names;

   foreach (Key key; SFW.keys())
   {
       string name = key.name();
       // string name = key.name().idup; // workaround for the issue
       names ~= name;
   }

   writeln("results:");
   foreach (name; names)
   {
       write(name, ", ");
   }
}

The resulting string array has strings that are overwritten with each other,
and in my case the results are similar to this:

Sun Microsystems, Sun Micros, Sun , Sun Micr, Sun, Sun Mic,...

And it goes like that for a hundred or so values, then switches to the next
registry key name and writes more overwwritten strings like that.

The commented out code is the workaround, however string to string assignment
should be safe without a need for .idup.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list