We have been having some strange problems on production installs. Things like screens clearing when they shouldn’t, applications slowing down when they didn’t before, and people’s computers running slower and slower until they close our application. When I was finally informed of this, I knew it had to be memory leaks.
So I fired up the excellent MemProfiler, I started digging into the issues. Aside from some of the other stupidity I was doing, I found a problem with a vendor’s component. We use Developers Express controls for EVERYTHING and we love them, not just the controls, but the company. When I found this issue with XtraReports not being GC’d after a dispose, I immediately created a small sample for them and submitted a ticket.
Less than 2 days later, they already have it fixed and ready to rock in the latest version. Not only am I impressed, but I am put at ease knowing that if a problem comes up, they are there to not only fix it, but fix it quickly and get it out to people.
This also happened a few months ago when their ASPxThemeDeployer wouldn’t work on my x64 Vista machine. I peeked at their code and saw why. Reported it and the VERY NEXT DAY they had a solution to the problem and pushed out an update.
I don’t have to worry. I know I am taken care of.
Let me preface this by saying I have no hate for VB.Net. When used properly, it is just as good as C#. However, VB as a whole has gotten a bad rep because generally the people that flock to it are the ones with no formal programming experience. Thus, they don’t really always know what they are doing. I’ve seen it a lot. I will also admit that I have done a lot of these horrendous things that I should have never done. In fact, this post is about something I did that turned out way bad when we fast forward a few years later (yes, it took that long for me to find this problem.)
Here is the problem and the solution I came up with over two years ago. We have a very large MDI application that has a lot of different forms. We didn’t want users opening up multiple copies of those forms, plus we wanted a neat way to bring certain forms to the foreground if they were already open.
My simple and what I thought was ingenious solution was to create a few functions that just check to see if the form is open… Something like this:
Public Function IsFormOpen(ByVal oFormName As Form) As Boolean Dim bAlreadyShown As Boolean = False Dim obj As Form For Each obj In Me.MdiChildren If obj.Name = oFormName.Name Then bAlreadyShown = True End If Next Return bAlreadyShown End Function
When I wrote that and it worked, it made sense to me that it would just pass in the type and I could get the name of the type and it would match and the world would be happy…
Now fast forward 2 years and we are getting some complaints of the application/whole computer being slow after running for a while. This smelled of memory leaks (which we have had lots of in the past). So I started doing my memory leak testing and now I am seeing all of our forms still being held open when they should be closed!
So turning off just my code debugging and putting a breakpoint in InitializeComponent() told me a big huge story. A story of how VB “helps” a developer by just doing things for them without them knowing. It seems that before going into the IsFormOpen function call, VB creates an object of that form type and passes in that object. That object never gets destroyed, either. Dispose on that object only gets called when the application exits. Nice, huh? So every time we open a form, we actually are creating two and only opening one. One simple change fixed it:
Public Function IsFormOpen(ByVal oFormName As System.Type) As Boolean Dim bAlreadyShown As Boolean = False Dim obj As Form For Each obj In Me.MdiChildren If obj.Name = oFormName.Name Then bAlreadyShown = True End If Next Return bAlreadyShown End Function
Incidentally, C# wouldn’t allow the first function to happen. It won’t even compile in any way, shape, or form (at least in the way I was trying to use it). It tells me the obvious, I was trying to use a Type as a variable. VB “helps” you with that…
Quite a while ago, I investigated the use of Linq to SQL in our one web service component that feeds a CMS that we package with out software. In my initial investigations, every time I would send an entity through the web service, I would get a circular reference error. This made sense since each relationship is bidirectional. If I have a family entity that has a relationship to a person entity, that person entity also has a reference to the family entity. This goes on infinitely. Those relationships are nice because you can grab one single person and then get data out of the family entity without having to do any joins or write a separate query. So what do you do?
There are a couple of options.
1. Make the data context serialization unidirectional. This will turn off all relationships. You will have to manually do your joins using Linq when the data reaches the other side of the transaction.
2. Change the parent access modifier of the relationship to Friend (VB) or Internal (C#). This leaves the child relationships intact but removes the parent relationships. There would no longer be a family relationship in the person entity.
I prefer option 2 simply because then I don’t have to rejoin my data every time I fetch it. I don’t need those child to parent relationships because I can build around it by making my web service functions return the family instead of just returning a person, even if I just request the person.
The next issue that I ran into was the updating existing records. Since adding new records takes a detached object and just inserts it as new, that isn’t an issue. The problem comes in the concurrency tracking that Linq to SQL uses. You either time stamp your record, or you allow it to check previous fields for concurrency. We don’t use the time stamp method. The way that most things work in the SQL world is that when you save, you either overwrite what is currently in the table, or you get an concurrency error that makes you input everything over again after getting a refresh from the database. This is not the way we like it. We like a merge scenario where I can make a couple changes to the record and someone else can make some other changes and we the save is made, both our changes make it to the database.
So, how do you fix this? It’s easy with Linq to SQL. It already has the merge saving built in, unlike ADO.Net. (The Entity Framework will do this as well if you set it up right.) Even though Linq to SQL already supports the saving, this won’t work in web services. The problem is that during serialization, the entity loses it’s data context. It’s the data context that actually tracks the changes in the record, not the entity itself. So once the data context loses it’s entity (gets detached), it has to be reattached in order to do the save. The problem with this is that the data context no longer knows what has changed in this entity, so it cannot do any concurrency validation. The key is to use the Attach method and fill in two of it’s parameters, one for current entity and one for the original entity. There are a couple ways you can do this, but I choose to add a base class where I could store the serialized entity (complete with children) and ship it back and forth.
No matter how you slice it, you are going to have the overhead of change tracking eating up bandwidth to and from the web service to the calling application. This can either be incredibly apparent by holding your original values in the session state and just passing it back to the web service, or you can serialize it in the entity object and just pull it out later. I use this method for simplicity. I actually just make a business class (actually, I already had one because all my validation is stored there in a generic, fun sort of way that allows it to just interact with IDataError) and in that business class, I just add a object holder and some functions to set the original value.
Here is a small sample of the base business class:
Imports System.Xml.Serialization Public Class BaseBusiness Private original As Object Public Sub SetOriginalValue(ByVal _OriginalValue As Object, ByVal originalType As System.Type) Dim sb As New StringBuilder() Using strw As New System.IO.StringWriter(sb) Dim SXO As New XmlSerializer(originalType) SXO.Serialize(strw, _OriginalValue) original = sb.ToString() End Using End Sub Public Function HasOriginalValue() As Boolean Dim bOriginal As Boolean = False If original IsNot Nothing Then bOriginal = True End If Return bOriginal End Function Public Function GetOriginalValue(ByVal originalType As System.Type) As Object If HasOriginalValue() Then Dim sXml As String = CStr(original) Dim fam As Object Using strr As New System.IO.StringReader(sXml) Dim SXO As New XmlSerializer(originalType) fam = SXO.Deserialize(strr) Return (fam) End Using Else Return (Nothing) End If End Function End Class
Notice the use of System.Type, this allows me to pass in any data type and serialize it on the fly without having to have special code in my entity class. This makes it appropriate for any class I need to track changes in, even in the entity framework if I so desire.
Here is the top level entity class where I use it:
Imports System.Xml.Serialization Partial Class Family Inherits BaseBusiness Private Sub OnLoaded() OriginalValue = Me End Sub Public Property OriginalValue() As Family Get Return CType((Me.GetOriginalValue(GetType(Family))), Family) End Get Set(ByVal value As Family) Me.SetOriginalValue(value, GetType(Family)) End Set End Property End Class
That is the only code that will need to be in the entity class in order for it to work. The best part of this is when the data gets shipped back and forth, it’s always accessible as the original entity, not a serialized XML string.
This was just a quick test I did when investigating this stuff today. Eventually, I am going to try to minimize the code in both classes some more and see if I can even strip out the XML Serialization to string and just store it as binary data.