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

Is this code right?

I feel i still do not understand maybe a bit a dispose pattern
So I have a question - is this code right? Is fs.Close() there where
it is right?
If I do understand it in place 'BBB' there can be a situation where
such member
obiect as fs can be finalised - so it may be wrong. Am I right or not?
But if i would close fs in place 'AAA' then I am not sure if a user of
this class
called a dispose() on it ? Am I wrong or right?

Thanks for Answer
JS

class JpgFileDecoder : IDisposable
{
private FileStream fs = null;
private ProgressEventHandler progressEventHandler = null;
private JpegDecoder jpegDecoder = null;

private AtalaImage atalaImage = null;
private ImageInfo imageInfo = null;

private bool isDisposed = false;

public JpgFileDecoder(string filePath,
ProgressEventHandler progressEventHandler)
{
this.progressEventHandler = progressEventHandler;
fs = new FileStream(filePath, FileMode.Open,
FileAccess.Read);
fs.Seek(0, SeekOrigin.Begin);
}

public AtalaImage AtalaImage
{
get
{
if (atalaImage == null)
{
atalaImage = jpegDecoder.Read(fs,
progressEventHandler);
}
return atalaImage;
}
set { }
}

public ImageInfo ImageInfo
{
get
{
if (imageInfo == null)
{
imageInfo = jpegDecoder.GetImageInfo(fs);
}
return imageInfo;
}
set { }
}
~JpgFileDecoder()
{
Dispose(false);
}

protected void Dispose(bool disposing)
{
if (disposing)
{
// AAA
// Code to dispose the managed resources of the class

}
//BBB
// Code to dispose the un-managed resources of the
class

fs.Close();
fs = null;

//

isDisposed = true;
}

public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}
Nov 20 '07 #1
3 1885
"Koliber (js)" <pr**************@poczta.onet.plschrieb im Newsbeitrag
news:91**********************************@f13g2000 hsa.googlegroups.com...
>I feel i still do not understand maybe a bit a dispose pattern
So I have a question - is this code right? Is fs.Close() there where
it is right?
If I do understand it in place 'BBB' there can be a situation where
such member
obiect as fs can be finalised - so it may be wrong. Am I right or not?
But if i would close fs in place 'AAA' then I am not sure if a user of
this class
called a dispose() on it ? Am I wrong or right?

Thanks for Answer
JS

class JpgFileDecoder : IDisposable
{
private FileStream fs = null;
<snip>
>
private bool isDisposed = false;

public JpgFileDecoder(string filePath,
ProgressEventHandler progressEventHandler)
{
this.progressEventHandler = progressEventHandler;
fs = new FileStream(filePath, FileMode.Open,
FileAccess.Read);
fs.Seek(0, SeekOrigin.Begin);
You shouldn't open your filestream an the get-accessor. If this property is
assigned twice, the former filestream remains open. Also calling programmers
wouldn't expect the object to enter open state on property-assignment.
Better is an Open method.
}
~JpgFileDecoder()
{
Dispose(false);
}
Actually it's not needed here, to have a finalizer, because you don't have
any unmanaged resources. It will raise performance issues (though maybe
small.)
This pattern can be handy, if a deriving class will reference unmanaged
resources directly, but maybe it is better to make your class sealed.
>
protected void Dispose(bool disposing)
{
if (disposing)
{
// AAA
// Code to dispose the managed resources of the class

}
//BBB
// Code to dispose the un-managed resources of the
class

fs.Close();
fs = null;
fs is a managed resource (=~ managed class that implements IDisposable). So
this should appear under // AAA.

So far
Christof Nordiek
>
//

isDisposed = true;
}

public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}
Nov 20 '07 #2
On 20 Lis, 16:13, "Christof Nordiek" <c...@nospam.dewrote:
"Koliber (js)" <profesor_kinb...@poczta.onet.plschrieb im Newsbeitragnews:91******************************** **@f13g2000hsa.googlegroups.com...
I feel i still do not understand maybe a bit a dispose pattern
So I have a question - is this code right? Is fs.Close() there where
it is right?
If I do understand it in place 'BBB' there can be a situation where
such member
obiect as fs can be finalised - so it may be wrong. Am I right or not?
But if i would close fs in place 'AAA' then I am not sure if a user of
this class
called a dispose() on it ? Am I wrong or right?
Thanks for Answer
JS


You shouldn't open your filestream an the get-accessor. If this property is
assigned twice, the former filestream remains open. Also calling programmers
wouldn't expect the object to enter open state on property-assignment.
Better is an Open method.
}
~JpgFileDecoder()
{
Dispose(false);
}

Actually it's not needed here, to have a finalizer, because you don't have
any unmanaged resources. It will raise performance issues (though maybe
small.)
This pattern can be handy, if a deriving class will reference unmanaged
resources directly, but maybe it is better to make your class sealed.
protected void Dispose(bool disposing)
{
if (disposing)
{
// AAA
// Code to dispose the managed resources of the class
}
//BBB
// Code to dispose the un-managed resources of the
class
fs.Close();
fs = null;

fs is a managed resource (=~ managed class that implements IDisposable). So
this should appear under // AAA.

So far
Christof Nordiek

I do not understand some things.

1. If i put fs closing under //AAA
then it will only be called when user calls Dispose() on my
class
so

What if class-user will forget about calling dispose?
a) fs.close() will never be called and fs will remain opdened forever?

Shouldn't I write code in this way that will make sure that
even if user will forgot about calling dispose finaliser
will do it - if so I think I should put filestream-closing code
under // BBB - so it can be called by finalisation time
so it will make sure that filestream will be closed

OR shouldn't I do that And must I belive that class-caler
will call dispose?

I am not sure how it should be done and it is real hard to
understand

thanx for answer
JS
Nov 20 '07 #3
"Koliber (js)" <pr**************@poczta.onet.plschrieb im Newsbeitrag
news:7b**********************************@n20g2000 hsh.googlegroups.com...
On 20 Lis, 16:13, "Christof Nordiek" <c...@nospam.dewrote:
>"Koliber (js)" <profesor_kinb...@poczta.onet.plschrieb im
Newsbeitragnews:91******************************* ***@f13g2000hsa.googlegroups.com...
protected void Dispose(bool disposing)
{
if (disposing)
{
// AAA
// Code to dispose the managed resources of the class
}
//BBB
// Code to dispose the un-managed resources of the
class
fs.Close();
fs = null;

fs is a managed resource (=~ managed class that implements IDisposable).
So
this should appear under // AAA.

So far
Christof Nordiek

I do not understand some things.

1. If i put fs closing under //AAA
then it will only be called when user calls Dispose() on my
class
so

What if class-user will forget about calling dispose?
a) fs.close() will never be called and fs will remain opdened forever?
Then, when the instance is eligible for collection also fs is eligible for
collection, and the finalizer will be called on fs (maybe even prior to the
finalizer of the instance).
That's the idea of the Dispose(bool) pattern and that is, why a class that
doesn't directly reference an unmanaged resource may not have a finalizer,
even if implements IDisposable (because it references an unmanaged resource
directly.)

Christof

Nov 20 '07 #4

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

Similar topics

3
by: netsurfer | last post by:
Hi: I am working on a project and need assistance, and would really appreciate any feedback on it. First part I'm banging my head against the wall on is about the amounts of the Amount fields...
6
by: christian9997 | last post by:
Hi I would be very helpful if someone could help me with this code. It works fine in IE but when I display it in Netscape or Firefox and I move the mouse from one menu to the other the gap...
2
by: PC User | last post by:
I found some VB code to printout a richtext field and I'm trying to adapt it to MS Access 2000. I think there might be some small changes to make the adaption between VB versions. Also my OS is...
5
by: Dan | last post by:
We have a simple site. It's a frameset with two frames a left and a right. The left frame is essentially a list of records from a database (using a server-side repeater control). When you click...
2
by: | last post by:
I have a breakpoint in an aspx page that I'm using to try to trap some code to see what's going on. I'm translating a page that is working in a traditional ASP page, which takes several session...
1
by: Neo Geshel | last post by:
I am having conflicting results with two pieces of identical code. One is an insert, the other is an update to a db. The first code, which works, is this: Sub Add3_Click(sender As Object, e As...
23
by: Edward Gregor | last post by:
Hi! I've written this code as a part of my program and I wonder if you would mind looking at the try_subtract_region and tell me if it well written. Thankful for help! struct region { int...
1
by: Eric | last post by:
When I run my script it gives error on the following line: strEmail = Right(strEmail, (Len(strEmail) - 1)) I enclose my code and the sample text file too Thanks,...
1
by: suneel271 | last post by:
code working in internet explorer but not in mozilla and iam getting erroe like window.event has no properties and here is the code <%@ page language="java" contentType="text/html;...
6
by: MrDeej | last post by:
Hello! I have code wich scans the through 10 computers on a LAN for files and import them into different tabels. This code aproximitly imports 10 000 files (with 1 to 20 rows of info) a day and is...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...

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.