Strange Crash - Stumped

Hey guys,

I’ve discovered something I can’t get my head around. I was going through my code and looking for potential problem spots. In one spot, I found I was basically doing this code:

    For each p as LinuxCompatiblePopUp in Switches
      p.deleteallrows
      
        For i as integer = 0 to Switch_IP_Dictionary.Count-1
          
          Try
            p.AddRow str(i+1)+" - "+Switch_IP_Dictionary.Value(Switch_IP_Dictionary.Key(i))
            p.RowTag(p.ListCount-1) = Switch_IP_Dictionary.Value(Switch_IP_Dictionary.Key(i))
          Catch
          End Try
          
        Next

// Other code….
    Next

This code is in a method that creates rows in a listbox and there’s a corresponding Pop-up menu for each row.

Now, I began to think, what if Switch_IP_Dictionary might happen to be Nil. That shouldn’t happen, but what if… So I changed the code:

    For each p as LinuxCompatiblePopUp in Switches
      p.deleteallrows
      
      If Switch_IP_Dictionary <> Nil Then
        
        For i as integer = 0 to Switch_IP_Dictionary.Count-1
          
          Try
            p.AddRow str(i+1)+" - "+Switch_IP_Dictionary.Value(Switch_IP_Dictionary.Key(i))
            p.RowTag(p.ListCount-1) = Switch_IP_Dictionary.Value(Switch_IP_Dictionary.Key(i))
          Catch
          End Try
          
        Next
        
      End IF

// Other code
      Next

Now, whenever the window that uses this routine opens, the app just hard crashes. The funny thing, this method hasn’t even been called when the crash occurs. I know because I put breakpoints in and the program never gets to the break point. It crashes beforehand. In fact during the initial opening of the window, the code execution NEVER gets to the If clause because the property “Switches” (which is an array) has no elements. Nothing in the for/next loop is executed during the opening. It’s only later. So the app crashes on code that doesn’t even execute!

If I comment out the If clause lines, things work perfectly.

And, if I do this:

    For each p as LinuxCompatiblePopUp in Switches
      p.deleteallrows
      
      If Switch_IP_Dictionary <> Nil Then
        
        For i as integer = 0 to Switch_IP_Dictionary.Count-1
          
          Try
            p.AddRow str(i+1)+" - "+Switch_IP_Dictionary.Value(Switch_IP_Dictionary.Key(i))
            p.RowTag(p.ListCount-1) = Switch_IP_Dictionary.Value(Switch_IP_Dictionary.Key(i))
          Catch
          End Try
          
        Next
        
      Else
      End IF

// Other code
      Next

Things work just fine as well - no crash. It is only when I have the IF/End If in the code when checking for a NIL dictionary.

I’m stumped….

    for each key as variant in switch_ip_dictionary.keys
         p.AddRow str(i+1)+" - "+Switch_IP_Dictionary.Value(key)
         p.RowTag(p.ListCount-1) = Switch_IP_Dictionary.Value(key)
    next

is how you should extract you values… I see no reason the NIL check should not work

personally I’d leave out the end if part and just put

If Switch_IP_Dictionary  Nil Then continue ' basically jump over everything straight to the NEXT

[quote=88117:@Dave S] for each key as variant in switch_ip_dictionary.keys p.AddRow str(i+1)+" - "+Switch_IP_Dictionary.Value(key) p.RowTag(p.ListCount-1) = Switch_IP_Dictionary.Value(key) next

is how you should extract you values… I see no reason the NIL check should not work

personally I’d leave out the end if part and just put

If Switch_IP_Dictionary Nil Then continue ' basically jump over everything straight to the NEXT [/quote]

That is a little more streamlined for extracting the keys. Good idea.

I agree that there’s no reason the NIL check should not work. But the entire app is hard crashing without any explanation with that If/End If in place. I’ll try the If/Then/Continue as you suggest. Wonder if it will crash using that syntax…

You said that if you comment out the if stmt, then there is no crash. While it is commented, put a breakpoint at the entry of this method, and see when that code is actually being executed. There may be some side effect happening that is triggering this code to run.

is there a crash report ?
or is it an assertion ?