[Issue 6194] [GSoC] Destructor gets called on object before it is copied when calling writeln()

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri Jun 24 04:28:19 PDT 2011


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



--- Comment #3 from Cristi Cobzarenco <cristi.cobzarenco at gmail.com> 2011-06-24 04:23:28 PDT ---
(In reply to comment #2)
> I'm not sure if it's a bug in DMD or Phobos. I'm inclined to it's Phobos bug.
> The loop variable 'a' in the reduced 'formattedWrite' is obviously escaping its
> scope, so calling the destructor is reasonable. However, this is essentially
> the implementation of std.format.formattedWrite:
> 
> 1. the arguments are taken address 
> 
>     foreach (i, arg; args)
>     {
>         funs[i] = &formatGeneric!(Writer, typeof(arg), Char);
>         // We can safely cast away shared because all data is either
>         // immutable or completely owned by this function.
>         argsAddresses[i] = cast(const(void*)) &arg;
>     }
> 
> 2. and then they are referred later for actual formatting.
> 
>         {
>             funs[currentArg](w, argsAddresses[currentArg], spec);
>             ++currentArg;
>         }
> 
> https://github.com/D-Programming-Language/phobos/blob/master/std/format.d#L302


This works for me:
 foreach (i, arg; args)
    {
        funs[i] = &formatGeneric!(Writer, typeof(arg), Char);

        static if(hasAliasing!(typeof(arg)))
        {
-            argsAddresses[i] = &arg;
+            argsAddresses[i] = &args[i];

        }
        else
        {
            // We can safely cast away shared because all data is either
            // immutable or completely owned by this function.
-            argsAddresses[i] = cast(const(void*)) &arg;
+            argsAddresses[i] = cast(const(void*)) &args[i];
        }
    }

Unfortunately we can't replace the foreach with a for loop because we can't do
typeof( args[ i ] ). Does any one have any idea how we could avoid copying the
arguments needlessly?

-- 
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