locked
An item with the same key was already added RRS feed

  • Question

  • For weeks now I've been getting the exception 'An item with the same key was already added' (I think that was the wording) - weird because the code verifies, before proceeding,  to make sure it WASN'T already present in the dictionary. 

    This exception was crashing my program several times day so I finally wrapped the code in a try-catch block but I still need to know what's wrong with my logic.

    The dictinionary as defined as <string, int>.

    if (!dicAllFilesAlreadyProcessedByPath.ContainsKey(f.aggregatePath.ToUpper())) {
    		try {
    			dicAllFilesAlreadyProcessedByPath.Add(f.aggregatePath.ToUpper(), 0);
    		} catch {
    		}
    	}

    Monday, August 26, 2013 4:16 PM

Answers

All replies

  • Are you sure the add statement is causing the coresponding catch?  VS is funny with the way exception handler work (try/catch).  When an exception occurs VS moves up th execution stack until the first exception handler is found.  So if you are missing exception handler in a function it will stop at the parent exception handler, even though it may not surround the location where the exception is occuring.  There may not be anything wrong with the code you are looking at.  The problem may be somewhere else.

    I think you need to add additional exception handler in other places in the code.


    jdweng

    Monday, August 26, 2013 4:27 PM
  • Are you sure the add statement is causing the coresponding catch?  VS is funny with the way exception handler work (try/catch).  When an exception occurs VS moves up th execution stack until the first exception handler is found.  So if you are missing exception handler in a function it will stop at the parent exception handler, even though it may not surround the location where the exception is occuring.  There may not be anything wrong with the code you are looking at.  The problem may be somewhere else.

    I think you need to add additional exception handler in other places in the code.


    jdweng

    Hmmm...Bear with me, I'm a bit slow. I'm thinking the reason you say that is that I didn't show enough code (I was trying to keep the post short and sweet). The production version has code inside the catch-block to pop up an error message in Wordpad, so I do think I've identified the line of code that throws the exception (correct me if I'm wrong):


     if (!dicAllFilesAlreadyProcessedByPath.ContainsKey(f.aggregatePath.ToUpper())) {
    try {
     dicAllFilesAlreadyProcessedByPath.Add(f.aggregatePath.ToUpper(), 0);
    } catch {
     bool verifiedThisKeyWasAlreadyInTheDictionary = dicAllFilesAlreadyProcessedByPath.ContainsKey(f.aggregatePath.ToUpper());
     if (oUserPrefs.popupExceptionsInwordpad) showMessageUsingWordPad("The following aggregate path was already in the dictionary (and verification status is: " + verifiedThisKeyWasAlreadyInTheDictionary.ToString() + "):\r\n\r\n" + f.aggregatePath);
    }
    }

    And the verification message shows as 'true' in case you're curious.


    • Edited by jal2 Monday, August 26, 2013 4:36 PM
    Monday, August 26, 2013 4:36 PM
  • The other weird thing is that I've used dictionaries for several years in literally dozens of programs and never had this problem.

    Monday, August 26, 2013 4:38 PM
  • the exception could of occur in code before the Try that wasn't in an exception handler.  Because you did have a try VS find the first Try and stops.

    Add another Try/Catch before the one you are stopping at and see if you stop in the new exception handler.

    public void Myfunction

    {

       Try                            <----- new exception handle from beginning of module to exisiting try

       { existing code}

        catch()

         {  ; }

       Try    <--------existing exception hanlder

       { ; }

        catch()

        { ; }

    }


    jdweng

    Monday, August 26, 2013 4:47 PM
  • The other weird thing is that I've used dictionaries for several years in literally dozens of programs and never had this problem.

    Are you accessing the dictionary from more than one thread?  Dictionary<T,U> is not thread safe - so if you're trying to do this from multiple threads, this can happen.  (In that case, ConcurrentDictionary<T,U>'s TryAdd method is probably safer...)


    Reed Copsey, Jr. - http://reedcopsey.com - If a post answers your question, please click Mark As Answer on that post. If you find a post helpful, please click Vote as Helpful.

    • Marked as answer by Caillen Tuesday, September 3, 2013 1:29 AM
    Monday, August 26, 2013 4:59 PM
  • You're correct that I don't have a try-catch block in the immediately preceding code. There is basically one try-catch block in my function.

    However, another factor not yet  mentioned is that the calling code has a try-catch, in other words the calling code calls the function like so:

    try{
    processFiles();
    }
    catch{
    }

    Based on this code, wouldn't the exception have bubbled up to the caller?

    Monday, August 26, 2013 5:03 PM
  • The other weird thing is that I've used dictionaries for several years in literally dozens of programs and never had this problem.

    Are you accessing the dictionary from more than one thread?  Dictionary<T,U> is not thread safe - so if you're trying to do this from multiple threads, this can happen.  (In that case, ConcurrentDictionary<T,U>'s TryAdd method is probably safer...)


    Reed Copsey, Jr. - http://reedcopsey.com - If a post answers your question, please click Mark As Answer on that post. If you find a post helpful, please click Vote as Helpful.

    Honestly from the very start it's been my suspicion that threading is the most likely cause of this problem. Trouble is, I'm not aware of multiple threads. It's a long-running process that runs on a single background-thread, so I certainly didn't intend to create a multitude of threads. But I suspect that's where the bug  lies - as hard as it is for me to believe, maybe my code somehow spins off  a second background thread without my realizing it.

    Of course this is hard to test because the problem is intermittent but I'll try to pin this down because I think you're right.

    Monday, August 26, 2013 5:12 PM
  • You can always print the Managed Thread ID of the current thread in there, and write it to a log file, etc.  That at least might help you diagnose the issue.

    If you've got more than one thread touching the dictinoary, that's likely the culprit.


    Reed Copsey, Jr. - http://reedcopsey.com - If a post answers your question, please click Mark As Answer on that post. If you find a post helpful, please click Vote as Helpful.

    • Marked as answer by Caillen Tuesday, September 3, 2013 1:27 AM
    Monday, August 26, 2013 5:15 PM