Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Bug in .Net 2.0's ArrayList.Clone?

Bug in .Net 2.0's ArrayList.Clone?

Scheduled Pinned Locked Moved C#
csharpjavajavascriptphpdatabase
11 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Firo Atrum Ventus

    Hi guys, I've just encountered a strange bug (I think?) in ArrayList.Clone() Here's my code :

    public ShipChromosome(ArrayList deckLength, ArrayList shipList, ArrayList allShipList)
    {
    deckList = deckLength;
    shipsInDeck = (ArrayList)shipList.Clone();
    allShips = (ArrayList)allShipList.Clone();//The culprit
    }

    #region IChromosome Members

    public IChromosome Clone()
    {
    return new ShipChromosome(deckList, shipsInDeck, allShips);
    }

    Okay, Here's the exception :

    System.ArgumentException was unhandled - Destination array was not long enough. Check destIndex and length, and the array's lower bounds.

    Some detail about parameters: deckList = ArrayList containing many int(s) shipsInDeck = ArrayList containing (deckList.Count) ArrayList(s) which hold many ShipObject allShips = ArrayList containing all ShipObject(s) in ShipsInDeck (Hey, I'm lazy!! :rolleyes: ) Any idea guys?

    Oxfords English < Official CCC Players Dictionary Excuse me for my improper grammar and typos. It's because English is my primary language, not my first language. My first languages are C# and Java. VB, ASP, JS, PHP and SQL are my second language. Indonesian came as my third language. My fourth language? I'm still creating it, I'll let you know when it's done! :-D

    L Offline
    L Offline
    Luc Pattyn
    wrote on last edited by
    #2

    We need more information and context. What are the counts/lengths of those collections? Does it go wrong the very first time, or just after a lot of similar operations? Are some of those collections growing all the time? What is this app about? :)

    Luc Pattyn [My Articles] Nil Volentibus Arduum iSad

    F 1 Reply Last reply
    0
    • L Luc Pattyn

      We need more information and context. What are the counts/lengths of those collections? Does it go wrong the very first time, or just after a lot of similar operations? Are some of those collections growing all the time? What is this app about? :)

      Luc Pattyn [My Articles] Nil Volentibus Arduum iSad

      F Offline
      F Offline
      Firo Atrum Ventus
      wrote on last edited by
      #3

      Luc Pattyn wrote:

      What are the counts/lengths of those collections?

      The first and second ArrayList always have 4 Items (setting) The third is changing every time.

      Luc Pattyn wrote:

      Does it go wrong the very first time, or just after a lot of similar operations?

      After a lot of similar operations.

      Luc Pattyn wrote:

      Are some of those collections growing all the time?

      Answered.

      Luc Pattyn wrote:

      What is this app about?

      Ship park scheduling(?), There's many decks(4) and many ships (hundred) and it must select the best combination for the ship (Every ship has different length and park cost)

      Oxfords English < Official CCC Players Dictionary Excuse me for my improper grammar and typos. It's because English is my primary language, not my first language. My first languages are C# and Java. VB, ASP, JS, PHP and SQL are my second language. Indonesian came as my third language. My fourth language? I'm still creating it, I'll let you know when it's done! :-D

      M L 2 Replies Last reply
      0
      • F Firo Atrum Ventus

        Luc Pattyn wrote:

        What are the counts/lengths of those collections?

        The first and second ArrayList always have 4 Items (setting) The third is changing every time.

        Luc Pattyn wrote:

        Does it go wrong the very first time, or just after a lot of similar operations?

        After a lot of similar operations.

        Luc Pattyn wrote:

        Are some of those collections growing all the time?

        Answered.

        Luc Pattyn wrote:

        What is this app about?

        Ship park scheduling(?), There's many decks(4) and many ships (hundred) and it must select the best combination for the ship (Every ship has different length and park cost)

        Oxfords English < Official CCC Players Dictionary Excuse me for my improper grammar and typos. It's because English is my primary language, not my first language. My first languages are C# and Java. VB, ASP, JS, PHP and SQL are my second language. Indonesian came as my third language. My fourth language? I'm still creating it, I'll let you know when it's done! :-D

        M Offline
        M Offline
        Mycroft Holmes
        wrote on last edited by
        #4

        Firo Atrum Ventus wrote:

        Ship park scheduling

        Amazing, I looked at something like this in the 90s, it required mapping the docks and defining segments, each ship would take up #n segments based on length, each 3rd (or random) segment had a services outlet and we had to get as many service outlets in use as possible. I'm astonished this has not already been done :) we could not get funding for the project at the time.

        Never underestimate the power of human stupidity RAH

        1 Reply Last reply
        0
        • F Firo Atrum Ventus

          Luc Pattyn wrote:

          What are the counts/lengths of those collections?

          The first and second ArrayList always have 4 Items (setting) The third is changing every time.

          Luc Pattyn wrote:

          Does it go wrong the very first time, or just after a lot of similar operations?

          After a lot of similar operations.

          Luc Pattyn wrote:

          Are some of those collections growing all the time?

          Answered.

          Luc Pattyn wrote:

          What is this app about?

          Ship park scheduling(?), There's many decks(4) and many ships (hundred) and it must select the best combination for the ship (Every ship has different length and park cost)

          Oxfords English < Official CCC Players Dictionary Excuse me for my improper grammar and typos. It's because English is my primary language, not my first language. My first languages are C# and Java. VB, ASP, JS, PHP and SQL are my second language. Indonesian came as my third language. My fourth language? I'm still creating it, I'll let you know when it's done! :-D

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #5

          Half answered. How many elements are in the third collection, the one that grows; initially, halfway, and when it fails? and how often are you doing all this cloning? and what happens to all those clones, are you storing them in yet another collection? insufficient information, best guess: LOH fragmentation. Never seen that exact error message though. :|

          Luc Pattyn [My Articles] Nil Volentibus Arduum iSad

          F 1 Reply Last reply
          0
          • L Luc Pattyn

            Half answered. How many elements are in the third collection, the one that grows; initially, halfway, and when it fails? and how often are you doing all this cloning? and what happens to all those clones, are you storing them in yet another collection? insufficient information, best guess: LOH fragmentation. Never seen that exact error message though. :|

            Luc Pattyn [My Articles] Nil Volentibus Arduum iSad

            F Offline
            F Offline
            Firo Atrum Ventus
            wrote on last edited by
            #6

            Luc Pattyn wrote:

            How many elements are in the third collection, the one that grows; initially, halfway, and when it fails?

            Let me rephrase my previous remark, The length is not growing, it depends on the value of ShipInDecks (I use this ArrayList just to make sure the new ship hasn't been added into ShipInDeck).

            Luc Pattyn wrote:

            and how often are you doing all this cloning?

            Cloning happens in crossover process (yes, this is Genetic Algorithm): 1. Clone 2 ShipChromosome (the class with mentioned code). //Where the error occured. 2. Do crossover from clones result.

            Luc Pattyn wrote:

            what happens to all those clones, are you storing them in yet another collection?

            Which clones? The class? public IChromosome Clone() create new object of ShipChromosome which should have the same value, hence the use of clone in the constructor. The array? I just cloned them from another instance of the class so that I can manage it freely.

            Luc Pattyn wrote:

            LOH fragmentation

            Looking into it. This error doesn't show up much (okay, only once), so I'm just doing this because I'm curious(ie. too many free time).

            Oxfords English < Official CCC Players Dictionary Excuse me for my improper grammar and typos. It's because English is my primary language, not my first language. My first languages are C# and Java. VB, ASP, JS, PHP and SQL are my second language. Indonesian came as my third language. My fourth language? I'm still creating it, I'll let you know when it's done! :-D

            1 Reply Last reply
            0
            • F Firo Atrum Ventus

              Hi guys, I've just encountered a strange bug (I think?) in ArrayList.Clone() Here's my code :

              public ShipChromosome(ArrayList deckLength, ArrayList shipList, ArrayList allShipList)
              {
              deckList = deckLength;
              shipsInDeck = (ArrayList)shipList.Clone();
              allShips = (ArrayList)allShipList.Clone();//The culprit
              }

              #region IChromosome Members

              public IChromosome Clone()
              {
              return new ShipChromosome(deckList, shipsInDeck, allShips);
              }

              Okay, Here's the exception :

              System.ArgumentException was unhandled - Destination array was not long enough. Check destIndex and length, and the array's lower bounds.

              Some detail about parameters: deckList = ArrayList containing many int(s) shipsInDeck = ArrayList containing (deckList.Count) ArrayList(s) which hold many ShipObject allShips = ArrayList containing all ShipObject(s) in ShipsInDeck (Hey, I'm lazy!! :rolleyes: ) Any idea guys?

              Oxfords English < Official CCC Players Dictionary Excuse me for my improper grammar and typos. It's because English is my primary language, not my first language. My first languages are C# and Java. VB, ASP, JS, PHP and SQL are my second language. Indonesian came as my third language. My fourth language? I'm still creating it, I'll let you know when it's done! :-D

              B Offline
              B Offline
              BobJanova
              wrote on last edited by
              #7

              As an aside, you should be using List<int>, List<List<ShipObject>> and List<ShipObject> here. Generics are good and make your life better. Though I wouldn't be sure that would solve this issue. Are you updating that collection from another thread? If the size is different when it allocates the clone to when it actually does the copy, something like that might happen. I'm not sure Clone is a thread safe operation.

              F 1 Reply Last reply
              0
              • B BobJanova

                As an aside, you should be using List<int>, List<List<ShipObject>> and List<ShipObject> here. Generics are good and make your life better. Though I wouldn't be sure that would solve this issue. Are you updating that collection from another thread? If the size is different when it allocates the clone to when it actually does the copy, something like that might happen. I'm not sure Clone is a thread safe operation.

                F Offline
                F Offline
                Firo Atrum Ventus
                wrote on last edited by
                #8

                BobJanova wrote:

                As an aside, you should be using List<int>, List<List<ShipObject>> and List<ShipObject> here. Generics are good and make your life better.

                Sure, but I'm currently in lazy mode now. I don't want to write all that comment for those new developers.

                BobJanova wrote:

                Are you updating that collection from another thread? If the size is different when it allocates the clone to when it actually does the copy, something like that might happen.

                Nope, this application is a prototype, everything runs in the main thread.

                Oxfords English < Official CCC Players Dictionary Excuse me for my improper grammar and typos. It's because English is my primary language, not my first language. My first languages are C# and Java. VB, ASP, JS, PHP and SQL are my second language. Indonesian came as my third language. My fourth language? I'm still creating it, I'll let you know when it's done! :-D

                B 1 Reply Last reply
                0
                • F Firo Atrum Ventus

                  BobJanova wrote:

                  As an aside, you should be using List<int>, List<List<ShipObject>> and List<ShipObject> here. Generics are good and make your life better.

                  Sure, but I'm currently in lazy mode now. I don't want to write all that comment for those new developers.

                  BobJanova wrote:

                  Are you updating that collection from another thread? If the size is different when it allocates the clone to when it actually does the copy, something like that might happen.

                  Nope, this application is a prototype, everything runs in the main thread.

                  Oxfords English < Official CCC Players Dictionary Excuse me for my improper grammar and typos. It's because English is my primary language, not my first language. My first languages are C# and Java. VB, ASP, JS, PHP and SQL are my second language. Indonesian came as my third language. My fourth language? I'm still creating it, I'll let you know when it's done! :-D

                  B Offline
                  B Offline
                  BobJanova
                  wrote on last edited by
                  #9

                  The generic type declaration is the comment, it is much clearer that you must put ints in there than if it's declared ArrayList and you can put anything in, but something will crash if you put the wrong type in. Honestly, in .Net 2 and up, you should only use the non-generic collections if you actually don't know what type you want to put in there.

                  F 1 Reply Last reply
                  0
                  • B BobJanova

                    The generic type declaration is the comment, it is much clearer that you must put ints in there than if it's declared ArrayList and you can put anything in, but something will crash if you put the wrong type in. Honestly, in .Net 2 and up, you should only use the non-generic collections if you actually don't know what type you want to put in there.

                    F Offline
                    F Offline
                    Firo Atrum Ventus
                    wrote on last edited by
                    #10

                    No, no, no, you don't understand. If I use a generics, I would have to put a mini documentation about generics (No, I'm not joking. I've been asked about the meaning of generics in my code TWICE. :doh: )

                    Oxfords English < Official CCC Players Dictionary Excuse me for my improper grammar and typos. It's because English is my primary language, not my first language. My first languages are C# and Java. VB, ASP, JS, PHP and SQL are my second language. Indonesian came as my third language. My fourth language? I'm still creating it, I'll let you know when it's done! :-D

                    B 1 Reply Last reply
                    0
                    • F Firo Atrum Ventus

                      No, no, no, you don't understand. If I use a generics, I would have to put a mini documentation about generics (No, I'm not joking. I've been asked about the meaning of generics in my code TWICE. :doh: )

                      Oxfords English < Official CCC Players Dictionary Excuse me for my improper grammar and typos. It's because English is my primary language, not my first language. My first languages are C# and Java. VB, ASP, JS, PHP and SQL are my second language. Indonesian came as my third language. My fourth language? I'm still creating it, I'll let you know when it's done! :-D

                      B Offline
                      B Offline
                      BobJanova
                      wrote on last edited by
                      #11

                      Sounds like you need to send those people on an elementary C# course. Generics are a fundamental part of the language and it's silly to not use them (and get all their benefits) because you have ignorami on the team.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups