473,406 Members | 2,769 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Any memory leak in this code snippet?

Could you advise me if there could be any memory leak in this C# code
snippet?:

http://pastebin.com/m2d2ded2d

As I understand, I have closed all risky objects: EventWaitHandle, and
Timer. Do I need to unregister the Elapsed event handler with the
delegate ( -= ) before disposing the timer? or it isn't necessary?

Thanks in advance,

Andrés
--
Nov 4 '07 #1
3 3329
Andres,

First off, that's a little more than a snippet. You should condense the
relevant sections of code down, as most people are not going to follow a
code sample that big.

Second, I ^strongly^ suggest that you look at the public member naming
guidelines, as your naming only makes it more difficult to follow things.

Those things being said, you definitely have issues not with memory
management, but how you are treating members which implement IDisposable.

You are storing a Timer and an AutoResetEvent in the CallerWatchDogX<X>
class, but it doesn't implement IDisposable. Rather, it calls the Dispose
method on itself in the DoWork method, which seems to, in the case of an
exception, start a timer, which fires an event on another thread, and then
notifies the calling thread (through the event) for no particular reason.

When you call Dispose on the IDisposable interface, the object should
not be used again. It's been disposed. In this case, you can continuously
use it here.

I would suggest using anonymous methods here, along with a using
statement to correct the situation in the DoWork method:

protected void DoWork()
{
// Place the timer and the auto reset event in the using statement.
using (AutoResetEvent event = new AutoResetEvent(false))
using (Timer timer = new Timer())
{
// Set up the timer.
timer.Interval = iMinutesToRetry * 60000;
timer.Elapsed = delegate(object sender, ElapsedEventArgs e)
{
Console.WriteLine("RELEASE_OBJ");
timer.Enabled = false;
event.Set();
Console.WriteLine("I WOKE MAIN UP");
};

bool bException = true;
Console.WriteLine("Entering on DoWork");

while (bException)
{
try
{
this.MakeCall();

Console.WriteLine("No exception!!");
bException = false;
}
catch (X oEx)//oEx)
{
Console.WriteLine("EXCEPTION!!!!!!!!");
if (this.fMethodForException != null)
{
this.fMethodForException.Invoke(oEx);
}
timer.Enabled = true;
Console.WriteLine("GONNA WAIT");
event.WaitOne();
Console.WriteLine("AWAKE!");
}
}
Console.WriteLine("END OF DOWORK");
}
}

This way, you localize the use of the timer and the event, and dispose
of them properly, not making them have to be disposed on the class level
(you have a problem if the class is created, but Dispose is never called).
With this, you can also get rid of the ReleaseObject method, the oTimer and
oHandle fields as well as the Dispose method.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

""Andrés G. Aragoneses [ knocte ]"" <kn****@NO-SPAM-PLEASE-gmail.comwrote
in message news:47**************@NO-SPAM-PLEASE-gmail.com...
Could you advise me if there could be any memory leak in this C# code
snippet?:

http://pastebin.com/m2d2ded2d

As I understand, I have closed all risky objects: EventWaitHandle, and
Timer. Do I need to unregister the Elapsed event handler with the
delegate ( -= ) before disposing the timer? or it isn't necessary?

Thanks in advance,

Andrés
--
Nov 4 '07 #2
Nicholas Paldino [.NET/C# MVP] escribió:
Andres,

(...)

This way, you localize the use of the timer and the event, and
dispose of them properly, not making them have to be disposed on the
class level (you have a problem if the class is created, but Dispose is
never called). With this, you can also get rid of the ReleaseObject
method, the oTimer and oHandle fields as well as the Dispose method.
Thanks for your advice. Then I suppose that a Timer gets disposed
properly although the Elapsed event is not unregistered firstly, right?

Regards,

Andrés [ knocte ]

--
Nov 5 '07 #3
Andres,

Yes. When dealing with events and IDisposable, you have to worry about
event handlers that are part of classes that implement IDisposable, not the
other way around (which is the case here). The timer is pointing to an
anonymous method, which doesn't implement IDisposable.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

""Andrés G. Aragoneses [ knocte ]"" <kn****@NO-SPAM-PLEASE-gmail.comwrote
in message news:47**************@NO-SPAM-PLEASE-gmail.com...
Nicholas Paldino [.NET/C# MVP] escribió:
>Andres,

(...)

This way, you localize the use of the timer and the event, and dispose
of them properly, not making them have to be disposed on the class level
(you have a problem if the class is created, but Dispose is never
called). With this, you can also get rid of the ReleaseObject method, the
oTimer and oHandle fields as well as the Dispose method.

Thanks for your advice. Then I suppose that a Timer gets disposed properly
although the Elapsed event is not unregistered firstly, right?

Regards,

Andrés [ knocte ]

--

Nov 5 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: thangamani.vaiyapuri | last post by:
Hi, The below code snippet shows memory issues in Vector's push_back method. #include <iostream.h> #include <vector> class Base {
8
by: ranjeet.gupta | last post by:
Dear All Is the Root Cause of the Memory corruption is the Memory leak, ?? suppose If in the code there is Memory leak, Do this may lead to the Memory Corruption while executing the program ? ...
4
by: Don Nell | last post by:
Hello Why is there a memory leak when this code is executed. for(;;) { ManagementScope scope = new ManagementScope(); scope.Options.Username="username"; scope.Options.Password="password";...
20
by: jeevankodali | last post by:
Hi I have an .Net application which processes thousands of Xml nodes each day and for each node I am using around 30-40 Regex matches to see if they satisfy some conditions are not. These Regex...
1
by: Barkha Shah | last post by:
Hi All, I am looking for a tool/utility that converts the address into source code line. Purpose is that I have written a basic mfc application that purposely has a memory leak.Now at the exit...
4
by: Peter | last post by:
..NET 2.0 Windows Forms app It seems like there's a memory leak in the DataGridView I have the following code: this.dvOpenOrders.Table = dt.GetOpenOrders(whse_code, this.dtpStartDate.Value,...
7
by: Brano | last post by:
Hi all, I have a VB.NET Dll that is invoked via BizTalk 2002 AIC over Http protocol. the Dll is making a connection using a 3rd party connector to a Unidata database (old legacy stuff) All I...
2
by: m0nkeymafia | last post by:
I have spent the past few weeks trying to figure a way around this problem, and have yet to find a good enough solution. Internet Explorer leaks memory when I update a div container using...
3
by: crazy420fingers | last post by:
I'm running a python program that simulates a wireless network protocol for a certain number of "frames" (measure of time). I've observed the following: 1. The memory consumption of the program...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.