473,412 Members | 2,599 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,412 software developers and data experts.

Managing/Disposing Memory in local scopes

AAJ
Hi

I have written a wrapper that encapsulates the Excel object model. As anyone
who's done a similar thing knows, its important to release the objects when
the app exits otherwise they still remain as a running process within
windows.

I've done this and so long as the code is called, it works well.

I then implemented Idisposable, and in my class put the clean up code in the
Dispose method.

Again this works OK so long as I call the dispose method when I'm done.

What I wanted, and what I thought would happen is that when the object went
out scope, it would be disposed of.

i.e.

private void button1_Click(object sender, EventArgs e)
{
ExcelWrapper TestSheet = new ExcelWrapper(@"C:\Test.XLS");
....
....
}

Dispose called here???
I thought the dispose method would have been called automatically as it
exited the function.

So what I'm getting at is, is there any way of ensuring that objects are
cleared automatically when they leave scope, or is it the programmers
responsibility.

thanks in advance

Andy
Apr 18 '06 #1
4 1406
Dispose is user level pattern, not a system pattern.
GC calls only finalizers. It does not care about anything else (or almost).
You must create a finalizer (aka C++ destructor) for your object, ~<your
object> where you clean up the unmanaged resources. You should, quite a
must, disable your finalizer if someone calls dispose.
Look more on the documentation regarding dispose and finalizer pattern.

Laura

"AAJ" <a.a.com> ha scritto nel messaggio
news:ur**************@TK2MSFTNGP02.phx.gbl...
Hi

I have written a wrapper that encapsulates the Excel object model. As
anyone who's done a similar thing knows, its important to release the
objects when the app exits otherwise they still remain as a running
process within windows.

I've done this and so long as the code is called, it works well.

I then implemented Idisposable, and in my class put the clean up code in
the Dispose method.

Again this works OK so long as I call the dispose method when I'm done.

What I wanted, and what I thought would happen is that when the object
went out scope, it would be disposed of.

i.e.

private void button1_Click(object sender, EventArgs e)
{
ExcelWrapper TestSheet = new ExcelWrapper(@"C:\Test.XLS");
...
...
}

Dispose called here???
I thought the dispose method would have been called automatically as it
exited the function.

So what I'm getting at is, is there any way of ensuring that objects are
cleared automatically when they leave scope, or is it the programmers
responsibility.

thanks in advance

Andy

Apr 18 '06 #2
AAJ
make you object to implement IDisposable if you didn't do so already. Then
in your method you can use the c# using statement

private void button1_Click(object sender, EventArgs e)
{
using(ExcelWrapper TestSheet = new ExcelWrapper(@"C:\Test.XLS"))
{
......
.....
}
}

TestSheet sheet object is going to be disposed as soon as it exits the using
statement scope. the using statement is compiled as try/finally block so of
you prefer you can write it that way.
--
HTH
Stoitcho Goutsev (100)

"AAJ" <a.a.com> wrote in message
news:ur**************@TK2MSFTNGP02.phx.gbl...
Hi

I have written a wrapper that encapsulates the Excel object model. As
anyone who's done a similar thing knows, its important to release the
objects when the app exits otherwise they still remain as a running
process within windows.

I've done this and so long as the code is called, it works well.

I then implemented Idisposable, and in my class put the clean up code in
the Dispose method.

Again this works OK so long as I call the dispose method when I'm done.

What I wanted, and what I thought would happen is that when the object
went out scope, it would be disposed of.

i.e.

private void button1_Click(object sender, EventArgs e)
{
ExcelWrapper TestSheet = new ExcelWrapper(@"C:\Test.XLS");
...
...
}

Dispose called here???
I thought the dispose method would have been called automatically as it
exited the function.

So what I'm getting at is, is there any way of ensuring that objects are
cleared automatically when they leave scope, or is it the programmers
responsibility.

thanks in advance

Andy

Apr 18 '06 #3


AAJ wrote:
private void button1_Click(object sender, EventArgs e)
{
ExcelWrapper TestSheet = new ExcelWrapper(@"C:\Test.XLS");
...
...
}

Dispose called here???
I thought the dispose method would have been called automatically as it
exited the function.
As others suggested, use "using".
So what I'm getting at is, is there any way of ensuring that objects are
cleared automatically when they leave scope, or is it the programmers
responsibility.


I use the following pattern to verify that Dispose was not forgotten:

public class Foo: IDisposable {
// stuff
public void Dispose() {
GC.SuppressFinalize(this);
}
~Foo() {
// Someone forgot dispose
try {
// Remember, that referenced objects may have been GC'ed already
// so the code may throw, but it's worth a try

// you can try logging instead, or whatever you wish to do
string msg = string.Format("{0}: Missing dispose", this);
throw new InvalidOperationException(msg);
} finally {
// Dispose anyway, may fail in destructor -- but worth a try
Dispose();
}
}
}

When a forgotten Dispose is spotted, by looking at logs or catched in
the debugger, I mixin a "CallStackOnConstruction" member on the class:

public class Foo: IDisposable {
...
string trace = CallStackOnConstruction.AsString();
}

and refine ~Foo to show that call-stack.

This pattern works *really* well for debugging forgotten Dispose,
atleast for me :)

--
Helge Jensen
mailto:he**********@slog.dk
sip:he**********@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-
Apr 19 '06 #4
AAJ
Many thanks for the quick responses. I never thought to try 'Using',
completely slipped my mind!!!

I'll have a look at finalisers as well, its something new to me, and its
always good to learn.

thanks again

Andy

"AAJ" <a.a.com> wrote in message
news:ur**************@TK2MSFTNGP02.phx.gbl...
Hi

I have written a wrapper that encapsulates the Excel object model. As
anyone who's done a similar thing knows, its important to release the
objects when the app exits otherwise they still remain as a running
process within windows.

I've done this and so long as the code is called, it works well.

I then implemented Idisposable, and in my class put the clean up code in
the Dispose method.

Again this works OK so long as I call the dispose method when I'm done.

What I wanted, and what I thought would happen is that when the object
went out scope, it would be disposed of.

i.e.

private void button1_Click(object sender, EventArgs e)
{
ExcelWrapper TestSheet = new ExcelWrapper(@"C:\Test.XLS");
...
...
}

Dispose called here???
I thought the dispose method would have been called automatically as it
exited the function.

So what I'm getting at is, is there any way of ensuring that objects are
cleared automatically when they leave scope, or is it the programmers
responsibility.

thanks in advance

Andy

Apr 19 '06 #5

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

Similar topics

47
by: Andrey Tatarinov | last post by:
Hi. It would be great to be able to reverse usage/definition parts in haskell-way with "where" keyword. Since Python 3 would miss lambda, that would be extremly useful for creating readable...
5
by: Noa Garnett | last post by:
I'm developing on C++, using visual studio 6.0 with service pack 5. I have a memory corruption while debugging. Some of the variables I'm using are suddenly set to zero while progressing along the...
3
by: Tony Johansson | last post by:
Hello Experts!! When you instansiate varaibles(object) you can do so in four different scops which are. Within a block which is called Local or block scope . Within a function which is called...
10
by: Kleenex | last post by:
Reason: I am working on an embedded project which has very limited memory (under 512 bytes, 60 or so of which is stack space), which translates into limited stack space. In order to save on stack...
13
by: MuZZy | last post by:
Hi, Just wanted to make sure i get it right: consider this class: // =========== START CODE ============= class Test { private SqlConnection con = null; public void Connect() { con = new...
1
by: Screenbert | last post by:
After finding nothing anywhere in google I am posting this so everyone can benefit by it. The formating is not pretty since I copied it from my word document, but you should benefit by it. ...
0
by: screenbert | last post by:
Managing DHCP Servers using C# They said it was impossible. It couldn't be done. But you can in fact manage DHCP servers using C#. This includes creating and deleting Scopes, SuperScopes,...
1
by: Michael Kansky | last post by:
I have a simple windows app written in VB.net. It has IE control on the form. Once IE control gets utilized by navigating to different www pages, Virtual memory used by the application rises and...
7
by: Tom Davis | last post by:
I am having a problem where a long-running function will cause a memory leak / balloon for reasons I cannot figure out. Essentially, I loop through a directory of pickled files, load them, and run...
29
by: Jerry Spence1 | last post by:
I'm rather confused as to whether something should be disposed of, or not. What is the general rule? How can you be sure of doing the right thing? I've heard about disposing unmanaged resources but...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.