deterministic cleanup in C++/CLI
-
I tried the following code
StreamWriter^ f = gcnew StreamWriter(L"d:\\listfile.txt"); f->Write(L"begin");
expecting a file containing the string "begin" to be created. To my surprise, only an empty listfile.txt was created. "begin" was not written. after a few trials, I realized I must explicitly callf->Close()
to get "begin" to be written. is it true that one of the advantages of C++/CLI over c# is that when an object goes out of scope, there is something called deterministic cleanup to close the object, or am I miseducated? For std::fstream, I surely don't have to call close() explicitly. -
I tried the following code
StreamWriter^ f = gcnew StreamWriter(L"d:\\listfile.txt"); f->Write(L"begin");
expecting a file containing the string "begin" to be created. To my surprise, only an empty listfile.txt was created. "begin" was not written. after a few trials, I realized I must explicitly callf->Close()
to get "begin" to be written. is it true that one of the advantages of C++/CLI over c# is that when an object goes out of scope, there is something called deterministic cleanup to close the object, or am I miseducated? For std::fstream, I surely don't have to call close() explicitly.You can use implicitly dereferenced variables. These variables use the Resource Acquistion Is Initialization (RAII) principle to do their clean up. Also, these variables have the same limiitations as automatic variables:
StreamWriter f(L"D:\\listfile.txt"); f.Write(L"begin"); ... // When "f" goes out of scope, it will be deterministically disposed of.
In order to obtain a tracking handle to an implicitly dereferenced variable, you must prefix it with "%".