Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Statement · Dec 12, 2010 at 05:10 PM · arraylistexceptionargumentexceptiontoarray

Why does List<T>.ToArray throw ArgumentException?

Why am I getting an argument exception telling me the destination array was not long enough while trying to make an array copy from a list?

List<MyCustomClass> asList = new List<MyCustomClass>();

// populate list etc etc. // ... // ...

// BANG! // ArgumentException: Destination array was not long enough. // Check destIndex and length, and the array's lower bounds.

MyCustomClass[] asArray = asList.ToArray();


Update:

It seems that another thread was updating the contents of the list unsynchronized so it could have been the cause of the bug. I can't tell for sure since we have since then moved on with another implementation but I am positive we had other bugs due to syncronization issues. It makes sense to think something bad can happen if the contents are changed while one thread is busy making the array. I'll hold off a couple more days until the deadline pressure release and report back if this was the sole cause in case anyone else happens to stumble across this same problem.

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Statement · Dec 12, 2010 at 05:40 PM 0
Share

You'd be thinking there would be any arguments passed to the method to get this exception :)

avatar image Mike 3 · Dec 13, 2010 at 05:22 AM 0
Share

Looks like a pretty valid error considering it's a sync issue. It'll create the array, then if you modify that array in the meantime, Array.Copy will be called with incorrect parameters and throw the exception

avatar image Statement · Dec 13, 2010 at 05:40 PM 0
Share

Yeah, at the point I wrote the first comment I hadn't made clear for me that there was code running on other threads. Actually the news that we HAD several threads came as a surprise to me.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Statement · Dec 15, 2010 at 01:45 AM

You need to check your code is thread safe. Use locks or other concurrent techniques.

// asynchronous access ahead, use locks: // one thread is doing: lock (asList) { asList.Add(new MyCustomClass()); }

// other thread is doing: lock (asList) { MyCustomClass[] asArray = asList.ToArray(); }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Using List.Count on Unity iPhone 2 Answers

A node in a childnode? 1 Answer

What's the proper way to see if a Vector3 List index is null? 1 Answer

Randomly remove list values 1 Answer

Can't add GameObjects to ArrayList 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges