D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 7696 - The Array container works improperly when using as a property
Summary: The Array container works improperly when using as a property
Status: RESOLVED INVALID
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: All Windows
: P2 normal
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-03-12 22:46 UTC by Heromyth
Modified: 2014-05-24 05:59 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Heromyth 2012-03-12 22:46:16 UTC
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 Damian 2014-05-24 05:59:12 UTC
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; }