Click here to Skip to main content
15,886,788 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: The "One" Pin
Lutosław25-Apr-16 11:38
Lutosław25-Apr-16 11:38 
GeneralRe: The "One" Pin
Matt T Heffron25-Apr-16 12:40
professionalMatt T Heffron25-Apr-16 12:40 
PraiseRe: The "One" Pin
Richard Deeming26-Apr-16 1:08
mveRichard Deeming26-Apr-16 1:08 
GeneralRe: The "One" Pin
Jörgen Andersson25-Apr-16 20:29
professionalJörgen Andersson25-Apr-16 20:29 
GeneralRe: The "One" Pin
Paulo Zemek25-Apr-16 20:44
mvaPaulo Zemek25-Apr-16 20:44 
GeneralRe: The "One" Pin
Paulo Zemek25-Apr-16 20:49
mvaPaulo Zemek25-Apr-16 20:49 
GeneralRe: The "One" Pin
David A. Gray1-Jul-16 21:46
David A. Gray1-Jul-16 21:46 
GeneralCaught in the trap again PinPopular
Bernhard Hiller21-Apr-16 20:48
Bernhard Hiller21-Apr-16 20:48 
A code fragment from a class used for testing purposes. The function cuts a snippet out of an image, sends it to some data transformation (the transformed data contain no reference to a bitmap), and notifies a different class that new data are available.
C#
Point position = _Motor.GetData().Position;
Rectangle rectangle = new Rectangle(position, Properties.ImageSize.Size);
Bitmap partialImage = _Bitmap.Clone(rectangle, _Bitmap.PixelFormat);
InfraredImage = _Converter.InfraredImageFromBitmap(partialImage);
OnImageReceived();

The function is called every few milliseconds. After a few milliseconds, Bad Things (TM) happened:
An unhandled exception of type 'System.OutOfMemoryException' occurred in System.Drawing.dll
- at the _Bitmap.Clone call.
How could that happen? Do I have to get rid of the partialImages? I added calls to Dispose. Set it explicitely to null. Called GC.Collect. Called GC.WaitForPendingFinalizers. Nothing helped.
Then I saw "rectangle = {X = 1630 Y = 0 Width = 80 Height = 100}". Uhm, _Bitmap is 1707 x 1280 pixels, isn't it? And 1630+80=1710: that's 3 pixels beyond the right border of the image. Actually an ArgumentException.
And then I remembered: with those managed wrappers of the unmanaged graphics API, any kind of error is translated into an OutOfMemoryException, regardless of the actual cause.
Why do I always have to learn that the hard way instead of remembering it immediately?
I fear the OutOfMemoryException might somewhen become the InnerException of a LostMyMindException ...

modified 22-Apr-16 2:58am.

GeneralRe: Caught in the trap again Pin
Rob Grainger22-Apr-16 1:16
Rob Grainger22-Apr-16 1:16 
GeneralRe: Caught in the trap again Pin
Richard Deeming22-Apr-16 1:22
mveRichard Deeming22-Apr-16 1:22 
JokeRe: Caught in the trap again Pin
Bernhard Hiller22-Apr-16 3:18
Bernhard Hiller22-Apr-16 3:18 
GeneralRe: Caught in the trap again Pin
Sascha Lefèvre22-Apr-16 4:11
professionalSascha Lefèvre22-Apr-16 4:11 
GeneralRe: Caught in the trap again Pin
Rob Grainger24-Apr-16 3:55
Rob Grainger24-Apr-16 3:55 
GeneralRe: Caught in the trap again Pin
Brisingr Aerowing24-Apr-16 13:18
professionalBrisingr Aerowing24-Apr-16 13:18 
JokeRe: Caught in the trap again Pin
Nicholas Marty28-Apr-16 0:53
professionalNicholas Marty28-Apr-16 0:53 
JokeRe: Caught in the trap again Pin
Marc Clifton9-May-16 8:43
mvaMarc Clifton9-May-16 8:43 
GeneralDysfunctional programming? Pin
PIEBALDconsult20-Apr-16 11:35
mvePIEBALDconsult20-Apr-16 11:35 
GeneralRe: Dysfunctional programming? Pin
Brisingr Aerowing20-Apr-16 13:31
professionalBrisingr Aerowing20-Apr-16 13:31 
GeneralRe: Dysfunctional programming? Pin
Tomas Takac20-Apr-16 20:54
Tomas Takac20-Apr-16 20:54 
GeneralRe: Dysfunctional programming? Pin
OriginalGriff20-Apr-16 21:45
mveOriginalGriff20-Apr-16 21:45 
GeneralRe: Dysfunctional programming? Pin
Super Lloyd20-Apr-16 22:28
Super Lloyd20-Apr-16 22:28 
GeneralRe: Dysfunctional programming? Pin
Nathan Minier21-Apr-16 1:39
professionalNathan Minier21-Apr-16 1:39 
GeneralRe: Dysfunctional programming? Pin
Rob Grainger22-Apr-16 1:30
Rob Grainger22-Apr-16 1:30 
GeneralRe: Dysfunctional programming? Pin
Snorri Kristjansson28-Apr-16 1:35
professionalSnorri Kristjansson28-Apr-16 1:35 
GeneralRe: Dysfunctional programming? Pin
j.peavey3-May-16 5:33
j.peavey3-May-16 5: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.