Click here to Skip to main content
15,885,792 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi,

I'v got a piece of code which works like I want it to. But I'm not sure what exactly happens behind.
I've got a dll which I use in my main application. This dll should not be deletable during runtime. Therefore I can open a stream at the program start and close it at the end:

System::IO::FileStream^ fs = System::IO::File::OpenRead(fileName); //start
fs->Close(); //end


But since I don't need the stream during runtime (I only access some .net classes from the dll directly) I can do it like this:

System::IO::File::OpenRead(fileName);


This also works. The file is open during runtime and cannot be deleted. Now my question: What exactly stays opened? There is no stream defined but the file is opened anyways. And how can I close it at the program's end without having a stream to close?

Thanks!
Mathias
Posted

The stream is defined you're just not grabbing a reference to it.
The stream will close when its finalizer runs.

Hope this helps,
Fredrik
 
Share this answer
 
Comments
mathias.woelfel 26-May-11 8:57am    
Ok, so I suspect its finalizer is called automatically? And who does that at which point?
Fredrik Bornander 26-May-11 9:26am    
It's essentially the destructor, it's run at some arbitrary point in time when the object is guaranteed not to be in use any more. That's why your first approach is better (in my opinion), because it is deterministic.
mathias.woelfel 26-May-11 9:57am    
Ok, that's what I wanted to know.
Thanks a lot!
Fredrik Bornander 26-May-11 11:46am    
Glad I could help.
Sergey Alexandrovich Kryukov 26-May-11 20:52pm    
Fredrik, what you say is correct, but it does not explain how the file gets locked. It can only be explained at the OS level. I voted 4 for your answer.

Please see my explanation.
--SA
This is explained at the OS level. Open operation you've demonstrated actually returns the file handle which can be used for other file operations (by non-OOP code as well). This operation can be done with exclusive access or not, exclusive being default. Each process has its unique address and handle space (if you pass the same handle to the same file to other process as identical arithmetic value it won't work as a file handle), so the handle is unique to the process. When the file is opened with exclusive access, the file as the object of the file system is marked by OS as busy; in this way it cannot be deleted or operated otherwise until the process which originally grabbed the handle either explicitly close it or is terminated. Non-graceful killing of the process (System.Diagnostics.Process.Kill) also releases the handle.

—SA
 
Share this answer
 
Comments
mathias.woelfel 31-May-11 10:11am    
Thank you for this detailed and interesting explanation!
Sergey Alexandrovich Kryukov 31-May-11 19:04pm    
You're welcome.
If you think so, please formally accept the answer (green button). You can accept more than one.
Thank you.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900