Bug 7696 – The Array container works improperly when using as a property
Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
Windows
Creation time
2012-03-12T22:46:00Z
Last change time
2014-05-24T05:59:12Z
Assigned to
nobody
Creator
bitworld
Comments
Comment #0 by bitworld — 2012-03-12T22:46:16Z
When an array container using as a property, the inner array (m_Children) must be set the capacity to a non-zero value. Otherwise, many operations (like reserve, insertBack, etc.) on the outter array (Children) works improperly.
The sample code is below.
=========================
public class TestClass
{
@property
{
public Array!(int) Children() { return m_Children; }
public void Children(Array!(int) value) { m_Children = value;}
}
private Array!(int) m_Children;
void populate()
{
m_Children.reserve(0); // the capacity can't be zero;
//Children.reserve(10); // no effect
writefln("%d", m_Children.capacity);
writefln("%d", Children.capacity);
//m_Children.clear();
for(int i=0; i< 5; i++)
{
Children.insertBack(i); // Here is the problem. It is affected by m_Children's capacity.
//m_Children.insertBack(i); // ok
}
writefln("Children length %d", Children.length);
}
this()
{
}
}
int main(string[] args)
{
TestClass testInstance = new TestClass();
testInstance.populate();
return 0;
}
Comment #1 by damianday — 2014-05-24T05:59:12Z
It's because your property is returning a copy of the array not the original.
Fixed by using a reference to the Array like so:
public ref Array!(int) Children() { return m_Children; }