Brain Spew - Neville Mehta's Blog

Wednesday, November 29, 2006

Output Caching in ASP.NET 2

Scott Guthrie has put up a great post on output caching in ASP.NET 2 and what he calls donut caching.

Controls.Clear() Doesnt Dispose In Win Forms!

We are currently doing memory leak testing on the project I am on that uses Windows Forms in .NET 1.1, and an interesting thing has come up. We noticed that when calling Controls.Clear() none of the controls in the collection were being disposed and hence causing handles to be leaked.
If you open Reflector and see the System.Windows.Forms.Control.ControlCollection.Clear() method, you will notice that it simply calls RemoveAt(int index) on each control in the collection.
So how do we get around this problem of controls not disposing?
Well, do we simply have to iterate through the collection and dispose each control like below to clear the controls collection?

foreach (Control control in this.Controls)
{
if (control != null)
control.Dispose();
}

NO! This wont work.
What will happen is that only about half the controls will actually be disposed.
The reason for this is that the Dispose() method removes the control from its collection and shifts all the controls down one index in the collection, and so when the foreach operator trys to get the next control, it goes to the next index which is really two infront of what it started on before disposing the previous control.
So, unfortunatley what we need to do is something like:

for (int i = 0; i < Controls.Count; i++)
{
if (Controls[0] != null)
Controls[0].Dispose();
}

Now, I dont know if this has remained the same in .NET 2 or 3 or event ASP.NET...I will leave that to you to play around with.

Saturday, November 25, 2006

Functional Programming Concepts and C# 3

Readify collegue Andrew Matthews brought my attention to a fantastic article on functional programming with languages such as C-omega and F# and thier influence on some of the features found in C# 3.0. Definitley worth a read!

Thursday, November 09, 2006

PathGeometry and StreamGeometry Mini-Language Syntax

I have been playing around with geometry in WPF lately and came accross the following syntax for drawing a curve:

<!-- clip -->


<Path
Data="M10,100 C 100,0 200,200 300,100 z"
Fill="Blue"
Stroke="Black"
StrokeThickness="4" />



<!-- clip-->

This is confusing because the Data property of the System.Windows.Shapes.Path object takes an object dervied from System.Windows.Media.Geometry.

It turns out that this is a special syntax for defining StreamGeometry objects or Figures in a PathGeometry object.

in the above example the "M" is saying is saying start at the absoloute position of (10, 100) then the "C" is saying draw a cubic bezier curve starting at (100, 0) then go to (200, 200) and ending at (300, 100). The "z" simply says to end the curve here and draw a line from the starting position to the current point.

For a more complete explaination, go to:
Path Marup Syntax

Monday, November 06, 2006

C# 3.0

I have also recently been quite interested to see whats coming up in C# 3 and came accross Sahil Malik's blog and his excellent series of posts on C# 3.0 which are short and to the point.

C Omega

Daniel Crowley-Wilson has recently been playing around with C Omega, which has got me a little curious to know more about it...you can have a look at what its all about here.

Sunday, November 05, 2006

Sound/Midi Not Working in Adobe Flash, YouTube etc. using CMedia sound card

About two months ago, I was playing around with the codecs on my Windows XP machine...I cant exactly remember why, but I think I was trying to install a new DivX one. I installed a few differnet players and codecs until I found the one I wanted. Everything seemed to be fine...until I went to YouTube and noticed that I was not getting any sound. I thought it was an Adobe Flash problem, so reinstalled it, but still no luck. I found that I could play MP3s normally but couldnt play MIDIs or any Flash sounds. It appeared to be playing as normal but no sound came out. I thought it may be a sound card problem (C Media) so I reinstalled the drivers...no luck.

I noticed in Control Panel under "Sounds and Audio Devices" (Windows XP) that my "default device" for MIDI Music Playback was blank and also my "default device" was blank. Even if I selected an option from the drop down list and pressed "Apply" then "OK", the settings would not be saved and next time I go back in, it would be blank again.

I also noticed that the speaker icon in the bottom right hand corner of the screen (in the task tray) had disappeared as well.

I was getting really frustrated and googled the problem and found that this was a very old problem with no resolution.

After playing around and tweaking alot of settings, I finally found the solution with a little help from an old friend RegMon. I found that Windows was looking for two keys in the registry and couldnt find them. These were:

HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/Windows NT/CurrentVersion/drivers32/midimapper

HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/Windows NT/CurrentVersion/drivers32/wavemapper

It seems that one of the programs I installed then uninstalled when looking for the codec/player that I wanted must have deleted these keys.

Anyway, the solution is that these two registry keys should be set to the following string values:

HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/Windows NT/CurrentVersion/drivers32/midimapper should be set to midimap.dll.

HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/Windows NT/CurrentVersion/drivers32/wavemapper should be set to msacm32.drv.

If you are still having problems, you may need to reinstall some default audio codecs the instructions found here.

Finally, everything is back to normal again! I can now select my voice playback and midi playback default device and the speaker icon is back in the system tray...and I can watch Flash videos and listen to MIDIs.