Click here to Skip to main content
15,888,113 members

The Weird and The Wonderful

   

The Weird and The Wonderful forum is a place to post Coding Horrors, Worst Practices, and the occasional flash of brilliance.

We all come across code that simply boggles the mind. Lazy kludges, embarrassing mistakes, horrid workarounds and developers just not quite getting it. And then somedays we come across - or write - the truly sublime.

Post your Best, your worst, and your most interesting. But please - no programming questions . This forum is purely for amusement and discussions on code snippets. All actual programming questions will be removed.

 
GeneralRe: Export to Excel Pin
leppie25-Oct-11 19:33
leppie25-Oct-11 19:33 
GeneralRe: Export to Excel Pin
Sundance Kid25-Oct-11 20:55
Sundance Kid25-Oct-11 20:55 
GeneralRe: Export to Excel Pin
GibbleCH26-Oct-11 4:13
GibbleCH26-Oct-11 4:13 
GeneralRe: Export to Excel Pin
Sundance Kid27-Oct-11 21:23
Sundance Kid27-Oct-11 21:23 
GeneralRe: Export to Excel Pin
BobJanova25-Oct-11 22:59
BobJanova25-Oct-11 22:59 
GeneralRe: Export to Excel Pin
leppie26-Oct-11 0:13
leppie26-Oct-11 0:13 
GeneralRe: Export to Excel Pin
BobJanova26-Oct-11 0:46
BobJanova26-Oct-11 0:46 
GeneralImmutables and indexers and finalizers, oh my! Pin
Member 205300620-Oct-11 6:44
Member 205300620-Oct-11 6:44 
Here is a lovely one I hand coded earlier. (names changed and many innocuous lines removed to protect the innocent code around it.)

C#
void ReduceCandidates(OblongCollection candidateOblongs)
{
    for (int index = 0; index < candidateOblongs.Count; ++index)
    {
        Oblong fittedOblong = candidateOblongs[index].Reduce(); // <---------- Object Disposed exception here
        // <snip code that uses fittedOblong>
    }
}

This looks like a simple piece of code that nothing can go wrong with.

Unfortunately it occasionally produces an object disposed exception on the line indicated.

OblongCollection and Oblong are both immutable classes with finalizers and implementing the dispose pattern.

What I think must be happening is a new Oblong is created in the OblongCollection indexer, the Oblong then goes out of scope and the Reduce method is called on it. In the Reduce method a method is called on the sole instance variable, but before the method is executed the finalizer kicks into action. This sees that the Oblong is no longer in scope, no instance variables are being used so disposes of the Oblong and calls the finalizer, trashing the object that the Reduce method is calling.

Took me a while to figure out what was happening - I did not want to just blindly change the code. Fixed code below. (Yes, I know a foreach would be better here, if possible, but I want to keep this simple).

C#
void ReduceCandidates(OblongCollection candidateOblongs)
{
    for (int index = 0; index < candidateOblongs.Count; ++index)
    {
        using (Oblong currentCandidate = candidateOblongs[index])
        {
            Oblong fittedOblong = currentCandidate.Reduce();
            // <snip code that uses fittedOblong>
        }
    }
}


Time to throw the Exception ElectrocuteCoder in every finalizer I guess Wink | ;-)
GeneralRe: Immutables and indexers and finalizers, oh my! Pin
Nagy Vilmos24-Oct-11 21:38
professionalNagy Vilmos24-Oct-11 21:38 
GeneralRe: Immutables and indexers and finalizers, oh my! Pin
Member 205300624-Oct-11 22:17
Member 205300624-Oct-11 22:17 
GeneralRe: Immutables and indexers and finalizers, oh my! Pin
Nagy Vilmos24-Oct-11 22:24
professionalNagy Vilmos24-Oct-11 22:24 
GeneralRe: Immutables and indexers and finalizers, oh my! Pin
Member 205300624-Oct-11 22:49
Member 205300624-Oct-11 22:49 
GeneralRe: Immutables and indexers and finalizers, oh my! Pin
Nagy Vilmos24-Oct-11 23:21
professionalNagy Vilmos24-Oct-11 23:21 
GeneralRe: Immutables and indexers and finalizers, oh my! Pin
Freak3028-Oct-11 1:30
Freak3028-Oct-11 1:30 
GeneralRe: Immutables and indexers and finalizers, oh my! Pin
BobJanova25-Oct-11 23:21
BobJanova25-Oct-11 23:21 
GeneralRe: Immutables and indexers and finalizers, oh my! Pin
Member 205300626-Oct-11 1:53
Member 205300626-Oct-11 1:53 
GeneralRe: Immutables and indexers and finalizers, oh my! Pin
BobJanova28-Oct-11 0:15
BobJanova28-Oct-11 0:15 
GeneralWe need more keys! Pin
Sander Rossel19-Oct-11 7:21
professionalSander Rossel19-Oct-11 7:21 
GeneralRe: We need more keys! Pin
fjdiewornncalwe19-Oct-11 7:30
professionalfjdiewornncalwe19-Oct-11 7:30 
GeneralRe: We need more keys! PinPopular
Sander Rossel19-Oct-11 7:45
professionalSander Rossel19-Oct-11 7:45 
GeneralRe: We need more keys! Pin
fjdiewornncalwe19-Oct-11 8:47
professionalfjdiewornncalwe19-Oct-11 8:47 
GeneralRe: We need more keys! Pin
Sander Rossel19-Oct-11 10:04
professionalSander Rossel19-Oct-11 10:04 
GeneralRe: We need more keys! Pin
BobJanova19-Oct-11 23:44
BobJanova19-Oct-11 23:44 
GeneralRe: We need more keys! Pin
Sander Rossel20-Oct-11 8:22
professionalSander Rossel20-Oct-11 8:22 
GeneralRe: We need more keys! Pin
fjdiewornncalwe20-Oct-11 9:33
professionalfjdiewornncalwe20-Oct-11 9:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.