Warning CA2000 : Microsoft.Reliability : In method ‘LogSetup.CreateLogSource(string, int)’, object ‘<>g__initLocal0’ is not disposed along all exception paths. Call System.IDisposable.Dispose on object ‘<>g__initLocal0’ before all references to it are out of scope. PCM.V1.Utilities

This code analysis error occurs for the following code:

Incorrect code
  1. private static void CreateLogSource(string logSourceName, int maxKiloBytes)
  2. {
  3.     if (!EventLog.SourceExists(logSourceName))
  4.     {
  5.         EventLog.CreateEventSource(logSourceName, logSourceName);
  6.         EventLog log = null;
  7.         using (log = new EventLog(logSourceName) { Source = logSourceName })
  8.         {
  9.             log.ModifyOverflowPolicy(OverflowAction.OverwriteAsNeeded, 0);
  10.             if (maxKiloBytes != 0)
  11.                 log.MaximumKilobytes = maxKiloBytes;
  12.             log.Clear();
  13.         }
  14.     }
  15. }

 

Solution: Remove the object initializer in the using statement:

 

Correct code
  1. private static void CreateLogSource(string logSourceName, int maxKiloBytes)
  2. {
  3.     if (!EventLog.SourceExists(logSourceName))
  4.     {
  5.         EventLog.CreateEventSource(logSourceName, logSourceName);
  6.         EventLog log = null;
  7.         using (log = new EventLog(logSourceName))
  8.         {
  9.             log.Source = logSourceName;
  10.             log.ModifyOverflowPolicy(OverflowAction.OverwriteAsNeeded, 0);
  11.             if (maxKiloBytes != 0)
  12.                 log.MaximumKilobytes = maxKiloBytes;
  13.             log.Clear();
  14.         }
  15.     }
  16. }

Advertisements

About vijayvepa

I'm a software consultant for Software Specialists Inc. currently working at Philips Respironics, Pittsburgh

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

%d bloggers like this: