Memory Leak

Recently I has received an email from a team member that she was getting a memory leak issue. She is very confident that it was a memory leak. I checked with her on what she was trying to accomplish on the code.

Background: The code was accessing some legacy database via COM component in a VB.Net project (Windows Application). They were trying to accessing a user information from the legacy database and post which some user related information. She highlighted me that the error was due to memory leak. It occurred some times and mostly when the user information was not retrieved correctly or when she use the logout button from the application.

Approach: I insisted to see the source code. I observed the code block which she suspected to be the problem area. I saw couple of things, firstly there was a public COM object declared and used across the application, It was initialized in the start of each code block and released at the end of the code block, there where too many interactions of the COM object created, also the COM object was released only at the last step of the code block, there were no exception handling for this COM object only a standard try catch block was available covering the whole 40+ lines of method.

Analysis: We suspected that the problem could be as follows:

  1. When an User not found or related exception (other then memory leak exception) occurred.
  2. The program flows to the catch block thus skipping the COM object release code block.
  3. So when next time application access same object, it was already in used.
  4. Thus creating the memory leak exception.

Solution: We just moved the release of COM object to the finally block.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s