views:

799

answers:

2

UPDATED QUESTION

Since the ctor is not supported by .NETCF (public FileStream(IntPtr handle, FileAccess access). Could you please suggest other ways of sharing large file in memory between managed and unmanaged code on a limited resource (RAM) platform. Basically I want to map the file in the upper region of 2GB user space (Win CE 5.0) outside of process space / heap. How can I do that in C#.

Also, do MemoryStream objects allocate space in heap or in memory mapped region on Win CE 5.0 ?

thanks...

ORIGINAL QUESTION

I am instantiating a FileStream Object (.NETCF , C#) using a file handle returned by native CreateFile() as below:

    //P/Invoke    
    [DllImport("coredll.dll", SetLastError = true)]
    public static extern IntPtr CreateFile(string lpFileName,
                                            uint dwDesiredAccess,
                                            uint dwShareMode,
                                            IntPtr lpSecurityAttributes,
                                            uint dwCreationDisposition,
                                            uint dwFlagsAndAttributes,
                                            IntPtr hTemplateFile);
// File handle received from native Win32 API
IntPtr ptr= CreateFile("myfile.txt",
                         0,
                         0,
                         0,
                         FileMode.Create,
                         0, 
                         IntPtr.Zero);

//Instantiate a FileStream object using handle (returned above) as parameter.
FileStream fs = new FileStream(ptr,FileAccess.ReadWrite);

The file will grow to large size > 500 KB or more. So, my questions are:

*1) Is there anything wrong with this way of doing things given that SafeFileHandle / Handle properties are not supported in .NETCF version ? Is there any better way of doing it (I am planning to use native memory mapped file handle with FileStream / MemoryStream) ?

2) Is the memory allocated by FileStream object fall under .NETCF garbage collector ? Or given that handle is of a memory mapped file created using native API, it (managed FileStream object and its resources) is out of purview of garbage collector ?*

Thanks in advance.

+3  A: 

Overall there is nothing wrong with this approach of using a native Create file and wrapping it in a FileStream object. This is a supported feature of FileStream.

n terms of garbage collection though there are really 2 things at play here.

  1. The memory associated with the FileStream object. Yes this will be garbage collected
  2. The handle which is a resource created with CreateFile. The FileStream object will take ownership of this handle and will free it when it is disposed (passively or actively).
JaredPar
+1 If the SafeFileHandle that was passed into the FileStream constructor was created with ownsHandle set to false, will the Dispose of the FileStream still close the handle from CreateFile that was passed into the SafeFileHandle constructor? I tried looking at reflector, but its a bit confusing.
SwDevMan81
+1  A: 

According to the documentation, the constructor you're planning on using isn't available in .NET CF.

Lee
Thanks. I didnt know that. Let me ask a different but related question. I will modify question above.
curiousone