0

I am writing a Windows kernel driver. I need to create a new I/O request and allocate my own memory for the input buffer.

// Create request
WDFREQUEST request;
status = WdfRequestCreate(WDF_NO_OBJECT_ATTRIBUTES, target, &request);
if (!NT_SUCCESS(status)) {
    goto exit;
}

// Allocate buffer for request
WDFMEMORY inputMemory;
status = WdfMemoryCreate(WDF_NO_OBJECT_ATTRIBUTES, PagedPool, 0, 1024, &inputMemory, NULL);
if (!NT_SUCCESS(status)) {
    goto exit;
}

// Assign input buffer to request
status = WdfIoTargetFormatRequestForIoctl(target, request, IOCTL_FOO, inputMemory, NULL, NULL, NULL);
if (!NT_SUCCESS(status)) {
    goto exit;
}

// Asynchronously send the ioctl request
WdfRequestSetCompletionRoutine(request, MyCompletionRoutine, NULL);
if (!WdfRequestSend(request, target, NULL)) {
    status = WdfRequestGetStatus(request);
    goto exit;
}

My question is, if WdfIoTargetFormatRequestForIoctl completes successfully, should I also perform WdfObjectDelete(inputMemory) in my cleanup, or will WdfObjectDelete(request) destroy both the memory and the request? Also, is the answer the same for both the error cleanup within the function and in the completion routine?

Andrew Sun
  • 4,101
  • 6
  • 36
  • 53
  • Why would I need to call `WdfRequestComplete`? The documentation says you should not call it for a request that you initialized yourself. – Andrew Sun Oct 07 '18 at 21:35
  • The link you provided is for a filter driver which is forwarding a request. I am creating a new request using `WdfRequestCreate`. Documentation says "if a driver calls WdfRequestCreate to create a request object, it must not call WdfRequestComplete for the request object." – Andrew Sun Oct 07 '18 at 21:57
  • Can you explain what the documentation here is saying then? I'm new to Windows drivers. https://learn.microsoft.com/en-us/windows-hardware/drivers/ddi/content/wdfrequest/nf-wdfrequest-wdfrequestcreate – Andrew Sun Oct 07 '18 at 22:08
  • The documentation doesn't clearly say that the `inputMemory` will be deleted or something but in my best knowledge if u delete the `request` object then `inputMemory` will be also deleted hierarchically, – Stubborn Oct 08 '18 at 12:18
  • @Stubborn Huh, that's exactly the opposite of what the previous commenter wrote. – Andrew Sun Oct 08 '18 at 20:41

1 Answers1

0

According to this the Driver object owns the memory, it will only be cleanup when you unloaded the driver.

if you can done with with the memory you should call WdfObjectDelete() to be not keep unused memory.

Baget
  • 3,318
  • 1
  • 24
  • 44