{"id":246,"date":"2008-01-09T20:50:58","date_gmt":"2008-01-09T20:50:58","guid":{"rendered":"https:\/\/driverentry.com.br\/en\/2008\/01\/09\/cleanup-and-close\/"},"modified":"2026-07-27T20:44:45","modified_gmt":"2026-07-27T20:44:45","slug":"cleanup-and-close","status":"publish","type":"post","link":"https:\/\/driverentry.com.br\/en\/2008\/01\/09\/cleanup-and-close\/","title":{"rendered":"CLEANUP and CLOSE"},"content":{"rendered":"<div style=\"text-align: right;\"><a href=\"https:\/\/driverentry.com.br\/2008\/01\/09\/cleanup-e-close\/\"><img loading=\"lazy\" decoding=\"async\" title=\"Em Portugu\u00eas\" src=\"https:\/\/driverentry.com.br\/en\/wp-content\/uploads\/2026\/07\/br.gif\" alt=\"\" width=\"21\" height=\"19\" \/><\/a><\/div>\n<p>I have written a <a href=\"https:\/\/driverentry.com.br\/en\/2007\/06\/18\/we-want-samples\/\">post<\/a> that describes the steps of how to create a simple driver. This driver simply keeps a list of strings it receives via writing IRPs and returns them via reading IRPs. Well, if you do not know what an IRP is, another <a href=\"https:\/\/driverentry.com.br\/en\/2007\/02\/12\/cool-but-what-is-an-irp\/\">post<\/a> tries to explain what they are and even describes the needed steps to use the <a href=\"http:\/\/www.osronline.com\/article.cfm?article=199\">IRP Tracker<\/a> to observe the IRPs coming and going. If we do a little test using the <strong>IRP Tracker<\/strong> on the sample driver that I have just commented, we will have an output similar to the figure below.<\/p>\n<div style=\"text-align: center;\"><img decoding=\"async\" src=\"https:\/\/driverentry.com.br\/en\/wp-content\/uploads\/2026\/07\/CleanupTrack.png\" alt=\"\" \/><br class=\"spacer_\" \/><\/div>\n<p>We can see all the IRPs that our device has received from <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ff550729(VS.85).aspx\">IRP_MJ_CREATE<\/a> to <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ff550720(VS.85).aspx\">IRP_MJ_CLOSE<\/a>. Among these IRPs, we can notice that some of them were not completed successfully. Because no routine has been designed to treat <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ff550718(VS.85).aspx\">IRP_MJ_CLEANUP<\/a>, these IRPs are completed with STATUS_INVALID_DEVICE_REQUEST. In this post I&#8217;m going to talk a little about this IRP and how your device interacts with the processes which have obtained a handle for it.<\/p>\n<p>To see the IRP details, double-click on the IRP line of the <strong>IRP Tracker<\/strong> so that the window below can appear. In this window you can see, among the other details, the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ff545834(VS.85).aspx\">FileObject<\/a> used in the IRP. This is helping us in this post during the tests.<\/p>\n<div style=\"text-align: center;\"><img decoding=\"async\" src=\"https:\/\/driverentry.com.br\/en\/wp-content\/uploads\/2026\/07\/ViewFileObject.png\" alt=\"\" \/><\/div>\n<h3>When does an IRP_MJ_CLEANUP take place?<\/h3>\n<p>The <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms724826(VS.85).aspx\">Object Manager<\/a> sends an <em>CLEANUP <\/em>IRP in order to notify the driver that the last handle for a given <em>FileObject<\/em> has been closed. As some of you may know, when an application uses <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/aa363858(VS.85).aspx\">CreateFile()<\/a> to get a handle to our device, this results in a <strong>FileObject<\/strong> creation. The subsequent operations using this handle will be linked to this <em>FileObject<\/em>. More details about this are going to be found in this <a href=\"https:\/\/driverentry.com.br\/en\/2007\/08\/29\/using-fileobject-and-fscontext\/\">post<\/a>.<\/p>\n<p>A <em>FileObject<\/em> does not have a direct relation with a handle. A handle might be duplicated  or even inherited from the parent process on creating a new process. The result of these actions is having multiple handles to be translated into the same <em>FileObject<\/em>. Thus, not always when an application calls the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms724211(VS.85).aspx\">CloseHandle<\/a> function, a <em>CLEANUP<\/em> or <em>CLOSE <\/em>IRP is sent to the driver.<\/p>\n<p>To follow the steps below, the sample program source code will be available for downloading at the end of this post. This program uses the example driver that have been built in another <a href=\"https:\/\/driverentry.com.br\/en\/2007\/06\/18\/we-want-samples\/\">post<\/a>; the driver source code can be downloaded from <a href=\"http:\/\/www.driverentry.com.br\/samples\/KernelEcho.zip\">here<\/a>. From the sources, you can compile and install the test driver. If you do not know how to do this, this <a href=\"https:\/\/driverentry.com.br\/en\/2006\/09\/11\/getting-started\/\">post<\/a> can help you. After installing the test driver, it would be interesting to execute the calls below with the help of a debugger, and thus, be able to observe the results on the <strong>IRP Tracker<\/strong> for ever executed line.<\/p>\n<p>It&#8217;s important to read the source comments below. I got lazy to duplicate this information here in the post.<\/p>\n<div style=\"font-family: Courier New; font-size: 9pt; color: black; background: #A4F64C; border: 1px outset; padding: 0 0 0 5px;\">\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\"><span style=\"color: green;\">\/****<\/span><\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\"><span style=\"color: green;\">***     main<\/span><\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\"><span style=\"color: green;\">**<\/span><\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\"><span style=\"color: green;\">**      I hope that everyone know this is the<\/span><\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\"><span style=\"color: green;\">**      application entrypoint. Otherwise,<\/span><\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\"><span style=\"color: green;\">**      you might be precipitaded on reading a<\/span><\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\"><span style=\"color: green;\">**      Windows driver blog.<\/span><\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\"><span style=\"color: green;\">*\/<\/span><\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\"><span style=\"color: blue;\">int<\/span> main(<span style=\"color: blue;\">int<\/span> argc, <span style=\"color: blue;\">char<\/span>* argv[])<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">{<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\">    HANDLE  h1, h2, h3;<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">    CHAR    szBuffer[<span style=\"color: purple;\">100<\/span>];<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\">    DWORD   dwBytes;<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">&nbsp;<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\">    <span style=\"color: green;\">\/\/-f--&gt; Here we are opening the first handle<\/span><\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">    <span style=\"color: green;\">\/\/      to our device. That is the step 1<\/span><\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\">    <span style=\"color: green;\">\/\/      on the IRP Tracker. Check out the FileObject<\/span><\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">    <span style=\"color: green;\">\/\/      value so that you can compare it in<\/span><\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\">    <span style=\"color: green;\">\/\/      future requests.<\/span><\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">    h1 = CreateFile(<span style=\"color: #a31515;\">\"\\\\\\\\.\\\\EchoDevice\"<\/span>,<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\">                    GENERIC_ALL,<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">                    <span style=\"color: purple;\">0<\/span>,<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\">                    NULL,<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">                    OPEN_EXISTING,<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\">                    <span style=\"color: purple;\">0<\/span>,<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">                    NULL);<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\">&nbsp;<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">    <span style=\"color: green;\">\/\/-f--&gt; Here we are opening the second handle<\/span><\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\">    <span style=\"color: green;\">\/\/      to our device. That is the step 2<\/span><\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">    <span style=\"color: green;\">\/\/      on the IRP Tracker. Check out the FileObject<\/span><\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\">    <span style=\"color: green;\">\/\/      value so that you can compare it in<\/span><\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">    <span style=\"color: green;\">\/\/      future requests.<\/span><\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\">    h2 = CreateFile(<span style=\"color: #a31515;\">\"\\\\\\\\.\\\\EchoDevice\"<\/span>,<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">                    GENERIC_ALL,<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\">                    <span style=\"color: purple;\">0<\/span>,<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">                    NULL,<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\">                    OPEN_EXISTING,<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">                    <span style=\"color: purple;\">0<\/span>,<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\">                    NULL);<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">&nbsp;<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\">    <span style=\"color: green;\">\/\/-f--&gt; Here we are throwing an IRP_MJ_READ for the<\/span><\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">    <span style=\"color: green;\">\/\/      first FileObject we have gotten. Step 3 on<\/span><\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\">    <span style=\"color: green;\">\/\/      IRP Tracker. Notice that the first FileObject<\/span><\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">    <span style=\"color: green;\">\/\/      is going to be used in this IRP.<\/span><\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\">    ReadFile(h1,<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">             szBuffer,<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\">             <span style=\"color: blue;\">sizeof<\/span>(szBuffer),<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">             &amp;dwBytes,<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\">             NULL);<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">&nbsp;<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\">    <span style=\"color: green;\">\/\/-f--&gt; Here we are throwing an IRP_MJ_READ for the<\/span><\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">    <span style=\"color: green;\">\/\/      second FileObject we have gotten. Step 4 on<\/span><\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\">    <span style=\"color: green;\">\/\/      IRP Tracker. Notice that the second FileObject<\/span><\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">    <span style=\"color: green;\">\/\/      is going to be used in this IRP.<\/span><\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\">    ReadFile(h2,<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">             szBuffer,<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\">             <span style=\"color: blue;\">sizeof<\/span>(szBuffer),<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">             &amp;dwBytes,<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\">             NULL);<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">&nbsp;<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\">    <span style=\"color: green;\">\/\/-f--&gt; The handle duplication is not notified<\/span><\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">    <span style=\"color: green;\">\/\/      to the driver. Only Object Manager \"has known\"<\/span><\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\">    <span style=\"color: green;\">\/\/      about that. There isn't any corresponding<\/span><\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">    <span style=\"color: green;\">\/\/      step on IRP Tracker.<\/span><\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\">    DuplicateHandle(GetCurrentProcess(),<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">                    h1,<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\">                    GetCurrentProcess(),<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">                    &amp;h3,<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\">                    <span style=\"color: purple;\">0<\/span>,<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">                    FALSE,<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\">                    DUPLICATE_SAME_ACCESS);<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">&nbsp;<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\">    <span style=\"color: green;\">\/\/-f--&gt; As the third handle was goten from the<\/span><\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">    <span style=\"color: green;\">\/\/      first handle duplication, the corresponding<\/span><\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\">    <span style=\"color: green;\">\/\/      FileObject is the same as the first handle.<\/span><\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">    <span style=\"color: green;\">\/\/      Step 5 on IRP Tracker. Notice that the first<\/span><\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\">    <span style=\"color: green;\">\/\/      FileObject is going to be used on this IRP.<\/span><\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">    ReadFile(h3,<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\">             szBuffer,<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">             <span style=\"color: blue;\">sizeof<\/span>(szBuffer),<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\">             &amp;dwBytes,<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">             NULL);<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\">&nbsp;<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">    <span style=\"color: green;\">\/\/-f--&gt; Because we have two handles for the first FileObject,<\/span><\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\">    <span style=\"color: green;\">\/\/      closing one of them will not gererate any notification<\/span><\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">    <span style=\"color: green;\">\/\/      to our device. There isn't any corresponding step<\/span><\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\">    <span style=\"color: green;\">\/\/      on IRP Tracker.<\/span><\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">    CloseHandle(h1);<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\">&nbsp;<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">    <span style=\"color: green;\">\/\/-f--&gt; That handle has not been duplicated, thus, when it is closed,<\/span><\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\">    <span style=\"color: green;\">\/\/      an IRP_MJ_CLEANUP followed by an IRP_MJ_CLOSE are going to be<\/span><\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">    <span style=\"color: green;\">\/\/      sent to the driver. Step 6 on IRP Tracker.<\/span><\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\">    CloseHandle(h2);<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">&nbsp;<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\">    <span style=\"color: green;\">\/\/-f--&gt; Closing that handle, we will have the same behavior<\/span><\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">    <span style=\"color: green;\">\/\/      seen during the h2 closing. From the driver<\/span><\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\">    <span style=\"color: green;\">\/\/      viewpoint, the first FileObject will be destroyed<\/span><\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">    <span style=\"color: green;\">\/\/      now. Step 7 on IRP Tracker.<\/span><\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\">    CloseHandle(h3);<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">&nbsp;<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\">    <span style=\"color: green;\">\/\/-f--&gt; And they have all lived happy ever after.<\/span><\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">    <span style=\"color: blue;\">return<\/span> <span style=\"color: purple;\">0<\/span>;<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\">}<\/pre>\n<\/div>\n<p>Getting a handle to an object assures us that this object will be valid until we close this handle. An object can only be destroyed by the system after all handles to it are closed. For this, the <em>Object Manager<\/em> maintains two counters, <strong>ProcessHandleCount<\/strong> and <strong>SystemHandleCount<\/strong>. The first one keeps the amount of open handles to an object in a given process. The other one maintains the sum of all <em>ProcessHandleCount<\/em> for the object in the system. These counters are decremented as these handles are closed. When they reach zero, a IRP_MJ_CLEANUP is generated.<\/p>\n<p>Usually an IRP_MJ_CLEANUP serves us as an event to cancel any asynchronous operation on the <em>FileObject<\/em> which is being finalized. These IRPs are linked to the threads that have launched them and any asynchronous IRP should be cancelled at this time.<\/p>\n<h3>But what is IRP_MJ_CLOSE used for?<\/h3>\n<p>Besides the reference counters above mentioned, there is also the <strong>ObjectReferenceCount<\/strong> which, besides being incremented when a new handle is obtained, it is also incremented when a reference is made using kernel functions like <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ff558678(VS.85).aspx\">ObReferenceObject()<\/a>, for example. These calls increment the <em>ObjectReferenceCount<\/em> without a new handle being generated. For those who have known <a href=\"http:\/\/en.wikipedia.org\/wiki\/Component_object_model\">COM<\/a>, this call has the similar behavior to <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms691379(VS.85).aspx\">AddRef()<\/a>. This allows the object to remain valid itself for the kernel, even after all handles to it have been destroyed. Anyway, when this counter reaches zero, then the IRP_MJ_CLOSE is sent.<\/p>\n<p>A driver can associate data structures for a given <em>FileObject<\/em> using the FsContext and FsContext2 pointers according to what I had  discussed in another <a href=\"https:\/\/driverentry.com.br\/en\/2007\/08\/29\/using-fileobject-and-fscontext\/\">post<\/a>. These structures can only be deallocated when the driver receives the IRP_MJ_CLOSE.<\/p>\n<h3>Operations after IRP_MJ_CLEANUP<\/h3>\n<p>When the driver receives an IRP_MJ_CLEANUP, it does not mean that the end is near. Other kernel components can still launch new IRPs for reading or writing, even after this event. Not to mention the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ff550766(VS.85).aspx\">IRP_MJ_INTERNAL_DEVICE_CONTROL<\/a> that can be exchanged among drivers.<\/p>\n<p>A very common scenario at the File Systems development is precisely about a reference to a <em>FileObject<\/em> that is maintained by the <strong>Cache Manager<\/strong>. Even when all handles are closed by the applications, the system still retains this refrence anticipating that some application may want to open the same file again. The <em>Cache Manager<\/em> has some <em>system threads<\/em> that perform the so-called <em>&#8220;delayed writing&#8221;<\/em>. This feature retains many writes to a file in a single operation aftermost in order to reduce the number of disk accesses, thus gaining performance. It is too often such writings taking place, once the file had all of its handles closed for applications, and then, these data being written after IRP_MJ_CLEANUP.<\/p>\n<p>In the end of it all, I hope to once again have helped more than hindered. Issues regarding the <em>Cache Manager<\/em>, <em>Memory Manager<\/em> and <em>File Systems<\/em> are not very trivial, but they are interesting enough to be read about and understand what the system has done before saying that everything is crap.<\/p>\n<p>Until next time!<\/p>\n<p class=\"download\"><a href=\"http:\/\/www.driverentry.com.br\/samples\/TestCleanup.zip\">TestCleanup.zip<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have written a post that describes the steps of how to create a simple driver. This driver simply keeps a list of strings it receives via writing IRPs and returns them via reading IRPs. Well, if you do not know what an IRP is, another post tries to explain what they are and even [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"pagelayer_contact_templates":[],"_pagelayer_content":"","footnotes":""},"categories":[1],"tags":[],"class_list":["post-246","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/driverentry.com.br\/en\/wp-json\/wp\/v2\/posts\/246","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/driverentry.com.br\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/driverentry.com.br\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/driverentry.com.br\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/driverentry.com.br\/en\/wp-json\/wp\/v2\/comments?post=246"}],"version-history":[{"count":2,"href":"https:\/\/driverentry.com.br\/en\/wp-json\/wp\/v2\/posts\/246\/revisions"}],"predecessor-version":[{"id":298,"href":"https:\/\/driverentry.com.br\/en\/wp-json\/wp\/v2\/posts\/246\/revisions\/298"}],"wp:attachment":[{"href":"https:\/\/driverentry.com.br\/en\/wp-json\/wp\/v2\/media?parent=246"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/driverentry.com.br\/en\/wp-json\/wp\/v2\/categories?post=246"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/driverentry.com.br\/en\/wp-json\/wp\/v2\/tags?post=246"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}