寒江独钓前辈的第一个例子的部分分析

来源:互联网 发布:macbook air装windows 编辑:程序博客网 时间:2024/05/16 08:20

分析了一下其源代码,不是很复杂,不过对其中的一段代码不太清楚。

// 打开设备对象
*status = IoGetDeviceObjectPointer(&name_str, FILE_ALL_ACCESS, &fileobj, &devobj);
if (*status == STATUS_SUCCESS)
ObDereferenceObject(fileobj);

书上是说如果IoGetDeviceObjectPointer 成功,需要调用ObDereferenceObject 来解除引用,否则会引起内存泄漏。 

始终没搞明白,怎么就内存泄漏了。 后来在网上搜了一下微软官网
https://technet.microsoft.com/zh-cn/ff549198(v=vs.80)
里面有详细的介绍。

This routine also returns a pointer to the corresponding file object. When unloading, a driver can dereference the file object as a means of indirectly dereferencing the device object. To do so, the driver calls ObDereferenceObject from its Unload routine, passing the file object pointer returned by IoGetDeviceObjectPointer

根据其描述,应该是在驱动卸载的时候,必须要调用ObDereferenceObject,将当初使用IoGetDeviceObjectPointer 函数的引用解除。 
所以个人认为其实该操作可以在卸载驱动的时候调用。 当然了,如果到那个时候调用的话,需要保存其入参,这样考虑的话,放在 IoGetDeviceObjectPointer  成功的时候调用,实现上是最方便的。 




通过 http://download.csdn.net/download/ftyou2000/3944539 里面的工具确实可以看出,我们的驱动对象下有一个设备对象。 (目前本机器只有一个串口)

0 0