a recommend from msdn about IoAllocateIrp

MS says:it’s recommended that free your own irp which is allocated by IoAllocateIrp.
but it should let I/O manager free the irp when using IoBuildxxx to allocate.

why is that?

See this article on Doron’s blog:

http://blogs.msdn.com/b/doronh/archive/2006/07/27/681179.aspx

-scott
@OSRDrivers

wrote in message news:xxxxx@ntdev…

MS says:it’s recommended that free your own irp which is allocated by
IoAllocateIrp.
but it should let I/O manager free the irp when using IoBuildxxx to
allocate.

why is that?

5771067@qq.com wrote:

MS says:it’s recommended that free your own irp which is allocated by IoAllocateIrp.
but it should let I/O manager free the irp when using IoBuildxxx to allocate.

why is that?

“Should” is not the right word here. When you use IoBuildXxx, the I/O
manager WILL free the IRP. It is trying to simulate the path taken by
I/O requests from user mode, where I/O manager owns the IRP. When you
use IoAllocateIrp, it doesn’t free the IRP, so you have to.

These are not just recommendations. These are the rules.


Tim Roberts, xxxxx@probo.com
Providenza & Boekelheide, Inc.

thank you both…
@Scott :
i thougth the caller to IoBuildAsynchronousFsdRequest does’t need to call IoFreeIrp by itself. after reading Doron’s blog, it turns out the caller have to .

@Tim:“When you
use IoAllocateIrp, it doesn’t free the IRP, so you have to.”
i can’t agree with that. at least there is one question it cant make sense: I/O manager hardly can tell the difference of Irp between IoAllocateIrp and IobuildSysXXXX

> thank you both…

@Scott :
i thougth the caller to IoBuildAsynchronousFsdRequest does’t need to call
IoFreeIrp by itself. after reading Doron’s blog, it turns out the caller
have to .

@Tim:“When you
use IoAllocateIrp, it doesn’t free the IRP, so you have to.”
i can’t agree with that. at least there is one question it cant make
sense: I/O manager hardly can tell the difference of Irp between
IoAllocateIrp and IobuildSysXXXX

Not quite true. If ou call IoAllocateIrp, you get an IRP with an empty
stack. When it comes back to your completion routine, it has an empty
stack, so if you don’t terminate the complwtion routine with
STATUS_MORE_PROCESSING_REQUIRED, the completion logic will attempt to
locate the higher-level completion routine by popping the empty stack.
The result Will Not Be A Pretty Sight, unless, of course, you find blue is
a pretty color.

The advice suggests that the IoBuild… methods might be doing
undocumented magic. The result being that the IRP thus created is given a
completion routine before being ssent to the lower-level driver; said
completion routine being responsible for disposing of that IRP.
joe


NTDEV is sponsored by OSR

Visit the list at: http://www.osronline.com/showlists.cfm?list=ntdev

OSR is HIRING!! See http://www.osr.com/careers

For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer

well,yes.if you put it in this way.

anyway.thank you all for replys :slight_smile:

> MS says:it’s recommended that free your own irp which is allocated by IoAllocateIrp.

but it should let I/O manager free the irp when using IoBuildxxx to allocate.

why is that?

Because only IopCompleteRequest can free the threaded IRP, and this function is only called after all drivers have their hands off the IRP.

Any IoFreeIrp in a driver for a threaded IRP is premature, without some important stuff from IopCompleteRequest being executed, and this stuff is - for a buffered read, memcpy() from SystemBuffer to UserBuffer, then fill UserIosb, signal UserEvent and so on.

A good advice: NEVER use threaded IRPs if you need a driver-originated IRP. NEVER call IoBuildxxx. Only call IoAllocateIrp and then fill manually (you can write a set of wrappers to do this).


Maxim S. Shatskih
Microsoft MVP on File System And Storage
xxxxx@storagecraft.com
http://www.storagecraft.com

> i can’t agree with that. at least there is one question it cant make sense: I/O manager hardly can tell

the difference of Irp between IoAllocateIrp and IobuildSysXXXX

Wrong.

IoAllocateIrp creates a non-threaded IRP.

IobuildSysXXXX creates a threaded IRP. So are all IRPs came from syscalls like ZwXxxFile, both from kernel and user mode.


Maxim S. Shatskih
Microsoft MVP on File System And Storage
xxxxx@storagecraft.com
http://www.storagecraft.com

@Maxim
i know that “IoAllocateIrp creates a non-threaded IRP.”
and the IobuildSysXXXX 's implement is base on IoAllocateIrp .so if we init the necessary members of the Irp allocated by IoAllocateIrp ,then we can make it a “threaded IRP”,

"> i can’t agree with that. at least there is one question it cant make sense:
I/O manager hardly can tell

the difference of Irp between IoAllocateIrp and IobuildSysXXXX
"
i means at this point(completing irp) the I/O manager() assume all the irp comes here is threaded.
that’s why it will crash since it acts like completing a threaded irp.

if im wrong,please correct me:),really appreciate that!

the term ?completing irp? I refered means at the stage after "unwing the complete routine?
and no one returns STATUS_MORE_PROCESSING_REQUIRED in their complete routine

If you mean the situation of “Io finally gets control after all completion routines are unwound” - then yes, this only occurs with threaded IRPs, and is finished with IopCompleteRequest.

With non-threaded IRP, the creator driver’s completion routine must either call IoFreeIrp itself, or cause the other code to do this, and return STATUS_MORE_PROCESSING_REQUIRED (i.e. “stop completion unwind”).


Maxim S. Shatskih
Microsoft MVP on File System And Storage
xxxxx@storagecraft.com
http://www.storagecraft.com

@Maxim.
yes. that’s what im trying to explain.
thanks a lot,looks like i understood it correctly

> MS says:it’s recommended that free your own irp which is allocated by

IoAllocateIrp.
but it should let I/O manager free the irp when using IoBuildxxx to
allocate.

why is that?

How about “Just because”? If that’s how the rules read, that’s what you
have to do.

If you don’t free an IRP you allocated by IoAllocateIrp, you will have
storage leak. If you free one created by IoBuild…, then you will get a
BSOD when some other component (that thinks the IRP is “live”) tries to
access it, especially if you have Special Pool enabled in the Driver
Verifier.
joe


NTDEV is sponsored by OSR

Visit the list at: http://www.osronline.com/showlists.cfm?list=ntdev

OSR is HIRING!! See http://www.osr.com/careers

For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer