Should I call Dispose on Process.GetCurrentProcess ()?

For example, see

How to get the current ProcessID?

No one was worried about calling Dispose on the object returned by System.Diagnostics.Process.GetCurrentProcess() . Should it be called? Please explain why.

+10
c # idisposable
source share
2 answers

Yes, and actually it is also important. If you see the actual source , you will see that Dispose not just inherited from Component , it does something too.

It seems to me, looking at this code, this is most important when EnableRaisingEvents set to true , since this is related to creating a wait descriptor. This descriptor must be released to prevent memory leak and processing.

+4
source share

This is a tough challenge.

You may not have to call Dispose on the Process instance that you received from Process.GetCurrentProcess() if you did not touch the Handle property, as well as some other vulnerabilities.

Let's look at the Process.Close method, which contains the essence of the Dispose logic.

  public void Close() { if (this.Associated) { if (this.haveProcessHandle) { this.StopWatchingForExit(); this.m_processHandle.Close(); this.m_processHandle = null; this.haveProcessHandle = false; } this.haveProcessId = false; this.isRemoteMachine = false; this.machineName = "."; this.raisedOnExited = false; this.standardOutput = null; this.standardInput = null; this.standardError = null; this.Refresh(); } } 

You can see that something real happens here only if the Process instance has a process handle. The Refresh method is not of interest to our topic.

If you look further, you will see that the process descriptor can be obtained (and thus saved) by the Process instance when accessing the Handle property. This is not the only case though!

  public IntPtr Handle { get { this.EnsureState(Process.State.Associated); return this.OpenProcessHandle().DangerousGetHandle(); } } 

As a general rule: if it implements IDisposable , you should call Dispose .

In your particular case, if you only touch the name of the current process or something innocent, you can omit the Dispose call and get away with it.

Here is an example:

  Process process = Process.GetCurrentProcess(); var fieldInfo = typeof(Process).GetField("haveProcessHandle", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); var v1 = fieldInfo.GetValue(process); //v1 is false. Explicit Dispose is not necessary. var processName = process.ProcessName; var v2 = fieldInfo.GetValue(process); //v2 is false. Explicit Dispose is not necessary. var processHandle = process.Handle; var v3 = fieldInfo.GetValue(process); //v3 is true. Bah. Explicit Dispose IS necessary from now on. 

I use reflection for one single reason: if you track the process variable through the Visual Studio debugger, it is going to view the properties and read the dreaded Handle property.

The Process class is a great example of a "bad design pattern" because it dramatically changes the state of an object in the get accessor.

+1
source share

All Articles