www.digitalmars.com         C & C++   DMDScript  

D - [BUG] foreach with new

reply Fabian Mathews <Fabian_member pathlink.com> writes:
//works fine:
for( int i = 0; i < 2; ++i )
{			
children[i] = new Node();
}

//this works, until you exit the loop/function
//then when you try to access children[0] you get an access violation
//as it has been cleaned up.
//changing child should change children[0] for example.
foreach( Node child; children )
{			
child = new Node();
}
Apr 20 2004
parent "Matthew" <matthew.hat stlsoft.dot.org> writes:
use inout

    foreach(inout Node child; children)

"Fabian Mathews" <Fabian_member pathlink.com> wrote in message
news:c64vuk$29iv$1 digitaldaemon.com...
 //works fine:
 for( int i = 0; i < 2; ++i )
 {
 children[i] = new Node();
 }

 //this works, until you exit the loop/function
 //then when you try to access children[0] you get an access violation
 //as it has been cleaned up.
 //changing child should change children[0] for example.
 foreach( Node child; children )
 {
 child = new Node();
 }

Apr 20 2004