|
Well....dispose does not guarantee that memory will be freed right?
As far as I understand, the GC is non-determinstic.
I was going through this article - http://www.devx.com/dotnet/Article/33167/0/page/3[^]. According to this Dispose( ) does not normally free managed memory directly. I had come across similar analysis somewhere else as well.
So I wonder why MS have called Dispose() . Is the author of that article incorrect?
OR perhaps there is some ambiguity regarding this method?
|
|
|
|
|
Abhinav S wrote: the GC is non-determinstic
correct
Abhinav S wrote: this article - http://www.devx.com/dotnet/Article/33167/0/page/3[^]
Haven't read that, and am not going to.
Abhinav S wrote: dispose does not guarantee that memory will be freed
correct.
Dispose is NOT about freeing the memory that holds the object; it is about other things the object may be holding on to. Such as unmanaged system objects (e.g. device contexts), unmanaged memory blocks, etc.
Conclusions:
- Dispose and GC are no alternatives, they complement each other.
- let the GC do its work (and don't interfere with it).
- call Dispose() for objects that have such method.
|
|
|
|
|
Luc Pattyn wrote: Conclusions:
- Dispose and GC are no alternatives, they complement each other.
- let the GC do its work (and don't interfere with it).
- call Dispose() for objects that have such method.
That is correct.
|
|
|
|
|
I think Luc Pattyn nailed the question, however I would like to point out something to avoid confusion.
Managed controls will eventually be handled by the GC. So disposing, though recommenden, is not absolutely necessary, however if you're working with unmanaged controls, make sure you dispose/delete/... in short cleanup everything yourself. Mostly if you're writing that code yourself you do, but, this also counts for unmanaged third party controls and that's often forgotten.
V.
|
|
|
|
|
There have been some interesting answers here, but I'm going to add a little wrinkle in that you may want to consider. What happens if any of the items in your collection have items that have event handlers that are still subscribed to from elsewhere? What do you think will happen to the items in the collection?
|
|
|
|
|
sir when i am running this code then process is throwing an exception ....and value of exit code becoming 1 ....i dont know why it is happening so ..plz help me ..
public void Restore()
{
try
{
string path;
path = filetext.Text;
StreamReader file = new StreamReader(path);
string input = file.ReadToEnd();
file.Close();
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = @"C:\Program Files\MySQL\MySQL Server 5.0\bin\mysqlimport";
psi.RedirectStandardInput = true;
psi.RedirectStandardOutput = false;
psi.Arguments = string.Format(@"-u{0} -p{1} -h{2} {3}",userid, paswd, server, comboBox1.Text);
psi.UseShellExecute = false;
Process process = new Process();
process = Process.Start(psi);
process.StandardInput.WriteLine(input);
process.StandardInput.Close();
process.WaitForExit();
process.Close();
MessageBox.Show("database is restored");
}
catch (IOException ex)
{
if (System.Diagnostics.Debugger.IsAttached)
{
Console.WriteLine(ex.ToString());
}
else
MessageBox.Show("Error , unable to Restore!");
}
}
|
|
|
|
|
What exception?
Where?
Please, try to give us all relevant information - help us, to help you!
Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water
|
|
|
|
|
when i am debugging the code and reach to process line and right click at process we find lot of exceptions and first is this .....
BasePriority = 'process.BasePriority' threw an exception of type 'System.InvalidOperationException'
|
|
|
|
|
Are you developing on Windows 98?
Windows 98 Platform Note: Setting the priority class to AboveNormal or BelowNormal causes an exception to be thrown.
Bastard Programmer from Hell
|
|
|
|
|
no sir i am devloping in windows 7 os
|
|
|
|
|
In that case, there are two conditions that can cause an InvalidOperationException (according to the documentation)
The process has exited.
-or-
The process has not started, so there is no process ID.
That's probably why there was an "WaitForInputIdle"; it needs to wait until the process actually started.
Bastard Programmer from Hell
|
|
|
|
|
yes process is exiting actualy its exit code is showing 1.....but i dont know why it is happening which line is to be corected ...so please analyse my code and provide appropriate solution
|
|
|
|
|
altafmohd wrote: but i dont know why it is happening which line is to be corected
Wait until it's done starting before you change it's priority. Take Google and see what "WaitForInputIdle" does, and don't comment lines that are "seemingly" useless.
altafmohd wrote: so please analyse my code and provide appropriate solution
That's what I have done.
Bastard Programmer from Hell
|
|
|
|
|
altafmohd wrote: catch (IOException ex)
{
if (System.Diagnostics.Debugger.IsAttached)
{
Console.WriteLine(ex.ToString());
}
else
MessageBox.Show("Error , unable to Restore!");
}
I object to this catch block: without a debugger, it swallows all available information and leaves no clue as to why the restore failed. Even if your users aren't interested in technical details, you have to support them and will be clueless as soon as the code fails for whatever reason. Write ex.ToString() to the log file!
|
|
|
|
|
but if i am not writing this in catch block then also same problem is coming ..i just posted this in catch block to check what is the problem in my code ...i dont know much about it ....so see the code is it right ?or what could be the problems
|
|
|
|
|
For creating backups of MySQL databases and restoring them, I use the GUI tools. I tried to do it with code of my own, and run into lots of problems: the low quality of the documentation (with your case here: what does exit code/error code/ErrCode 1 mean?), characters outside the 7bit-ASCII range being changed into nonsense, etc. Consequently, I try to avoid MySQL nowadays, even though I was a fan of it in the past.
In case you have to stick with MySQL: try to restore the database with the MySQL GUI tools, perhaps you could get some better information there.
|
|
|
|
|
What would you expect foo to be after this code fragment runs? Post your answer without first peeking at the docs!
string badNumericString = "Bogus";
Decimal foo = Decimal.MinValue;
bool status = Decimal.TryParse (badNumericString, out foo);
Answer:Decimal.MinValue (since the parse failed)
- Something else (if so, what?)
/ravi
|
|
|
|
|
Well, ok I have peeked to see if the behaviour is the same as Int32.TryParse and it is. Importantly the out keyword requires that the variable passed as the argument is definitely assigned by the method and whether the method returns true or false has no bearing on that requirement.
So my answer is I would expect foo to be assigned but whether or not that changes the value depends on whether the method assigns MinValue or something else.
Alan.
|
|
|
|
|
Alan N wrote: the out keyword requires that the variable passed as the argument is definitely assigned by the method Yes, absolutely. And I was hoping TryParse() would assign the incoming value of foo when the parse failed. When it failed to do so, I decided to write my own SafeTryParse() implementation that would do just that, only to be chastised by the compiler - an out parameter is always assumed to be uninitialized and therefore its value can't be used.
/ravi
|
|
|
|
|
I did that test, only to get my wrist slapped too!
Alan.
|
|
|
|
|
Heh. So this is what I ended up creating:
public void SafeTryParse
(string stringValue,
Decimal? defaultValue,
out Decimal value)
{
if (!Decimal.TryParse (stringValue, out value) && defaultValue.HasValue) {
value = defaultValue.Value;
}
} /ravi
|
|
|
|
|
Or change out to ref and do this:
private bool SafeTryParse(string s, ref decimal result) {
decimal temp;
bool ok = Decimal.TryParse(s, out temp);
if (ok) result = temp;
return ok;
}
Alan
|
|
|
|
|
Great minds think alike.
/ravi
|
|
|
|
|
Hmm. That isn't very safe, result could contain an unintended value:
decimal val;
val=some_garbage;
someOperations(val);
bool OK=SafeTryParse("foo", ref val);
One shouldn't give two roles to a single variable, so I'd rather have two overloads both using the out keyword:
private bool SafeTryParse(string s, out decimal result);
private bool SafeTryParse(string s, out decimal result, decimal dflt);
You can still get the "overwrite when parse succeeds" effect by passing <ocde>val twice to the latter.
|
|
|
|
|
I would argue that forgetting to set a valid initial value is programmer error, however I now like the idea of an explicit default. That's what I initially implemented (link[^]), but like Alan, switched to the 2 parameter ref version. I'm going to switch back.
Thanks,
/ravi
|
|
|
|