J
Jon
Hi,
I'm having a problem with deserialising from a file.
I have two buttons on a Windows form (see simplified example code below). The first serialises an
integer value to a file and the second deserialises the same data from the file.
The first function works fine, but the second causes a runtime exception in the "finally" block.
Stepping through in the debugger, stream is set to something sensible in the try section, but
becomes null as soon as it leaves the try section and enters into the finally section, and calling
Close() on the null causes the exception. Curiously, this exception does not occur in the first
function.
1) For each case, does the stream get closed automatically when if falls out of the try{} scope? If
so, I don't need to have stream.Close() in the finally block. I ask this since when opening a
connection to a database, it is best to explicitly call close, since the GC may not run for a long
time after falling out of scope. I know that closing a stream is not the same as closing a DB
connection, but wondered if similar principles apply?
2) Why does only the second function set stream to null, but not the first function?
I'm using VS 2008 Express, targeting .NET 2.0 on an XP pro PC.
private void btnSaveData_Click(object sender, EventArgs e){
try{
Stream stream = File.Open(@"C:\test.dta", FileMode.Create);
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(stream, testInt);
}catch(Exception ex){
// TO DO: add code to recover from exception
}finally{
stream.Close();
}
}
private void btnReadData_Click(object sender, EventArgs e){
try{
Stream stream = File.Open(@"C:\test.dta", FileMode.Open);
BinaryFormatter formatter = new BinaryFormatter();
testInt = (int)formatter.Deserialize(stream);
}catch(Exception ex){
// TO DO: add code to recover from exception
}finally{
stream.Close(); // "stream" is always null here, so get an exception
}
}
I'm having a problem with deserialising from a file.
I have two buttons on a Windows form (see simplified example code below). The first serialises an
integer value to a file and the second deserialises the same data from the file.
The first function works fine, but the second causes a runtime exception in the "finally" block.
Stepping through in the debugger, stream is set to something sensible in the try section, but
becomes null as soon as it leaves the try section and enters into the finally section, and calling
Close() on the null causes the exception. Curiously, this exception does not occur in the first
function.
1) For each case, does the stream get closed automatically when if falls out of the try{} scope? If
so, I don't need to have stream.Close() in the finally block. I ask this since when opening a
connection to a database, it is best to explicitly call close, since the GC may not run for a long
time after falling out of scope. I know that closing a stream is not the same as closing a DB
connection, but wondered if similar principles apply?
2) Why does only the second function set stream to null, but not the first function?
I'm using VS 2008 Express, targeting .NET 2.0 on an XP pro PC.
private void btnSaveData_Click(object sender, EventArgs e){
try{
Stream stream = File.Open(@"C:\test.dta", FileMode.Create);
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(stream, testInt);
}catch(Exception ex){
// TO DO: add code to recover from exception
}finally{
stream.Close();
}
}
private void btnReadData_Click(object sender, EventArgs e){
try{
Stream stream = File.Open(@"C:\test.dta", FileMode.Open);
BinaryFormatter formatter = new BinaryFormatter();
testInt = (int)formatter.Deserialize(stream);
}catch(Exception ex){
// TO DO: add code to recover from exception
}finally{
stream.Close(); // "stream" is always null here, so get an exception
}
}