stl set::insert 会导致 BSOD 吗?

发布于 2024-12-16 19:28:57 字数 2302 浏览 0 评论 0原文

我编写了一个“filemon”实用程序,它基本上记录一段时间内打开的文件。现在,我在“filemon”实用程序中精确地定义了这两个函数:

set<wstring> wstrSet;

// callback - get notified by callback filter driver on any file open operation
void CbFltOpenFileN(
                                    CallbackFilter* Sender,
                                    LPWSTR FileName,
                                    ACCESS_MASK DesiredAccess,
                                    WORD FileAttributes,
                                    WORD ShareMode,
                                    DWORD CreateOptions,
                                    WORD CreateDisposition )
 {
      // don't log for directories
      if (FileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
          return;
      }
      wstring wstr = FileName;
      wstr.append(L"\n");
      //wstrSet.insert(wstr); // as soon as I un-comment this line I start getting BSOD in multiple execution of this utility
 }


// Read all paths stored in the set and write them into the log file
void WritePathsToLog() {
    typedef set<wstring>::const_iterator CI;
    printf("\nNo. of files touched ===> %d\n\n", wstrSet.size());
    for (CI iter = wstrSet.begin(); iter != wstrSet.end(); iter++) {
        fputws((*iter).c_str(), logFile);
    }
}

基本上,此代码的作用是“filemon”实用程序与回调筛选器驱动程序交互,每当任何进程触及文件时,驱动程序都会向“filemon”实用程序报告相应的文件名通过 CbFltOpenFileN 函数。

现在的问题是,这个“filemon”实用程序在 win7 x64 机器 4GB 机器上运行良好,但是一旦我取消注释 CbFltOpenFileN 函数中的最后一行,即开始在集合中插入报告的文件名,然后我开始出现 BSOD,主要是 BugCheck 0xCC 和有时使用 BugCheck 0x50,这基本上表明“系统正在尝试访问已释放的内存”

PS 在带有 8GB RAM 的 win7 x64 上我没有看到任何问题无论 CbFltOpenFileN 函数中的最后一行是否被注释,都是如此。

目前,“wstrSet”正在使用默认分配器,因此我在声明 set wstrSet; 时是否需要使用特定的分配器?如果是的话,有人可以告诉我如何&为什么?

  • 让我在这里分享更多信息:
    • 我使用的是VS2010
    • 我的实用程序是一个针对 x86 平台的 32 位应用程序
    • 我使用的fileystem过滤驱动是Eldos公司提供的callabck过滤驱动。
    • 现在我正在使用模拟器测试此实用程序,该模拟器会生成大量文件,然后启动“filemon”实用程序,然后它会触及所有这些文件,最后停止“filemon”实用程序。该模拟器重复此过程 25 次。
    • 现在,对于最后一行被注释的情况,我创建了 25 次空日志文件,因为我没有在集合中插入任何内容,但“filemon”实用程序也不会导致任何 BSOD
    • 但是对于最后一行没有注释的情况,我每次都会使用路径输入创建日志文件,因为现在我在集合中插入路径,但在前几次迭代期间,比如第二次或第三次或第六次,它本身是“filemon”实用程序遇到这个 BSOD 情况。

我很难在调试模式下重现此问题,因为我的模拟器负责“filemon”实用程序的启动/停止,并且我需要多次运行它才能重现该问题。

希望这个添加的信息有帮助!

谢谢和问候, 萨钦

I have written a 'filemon' utility which basically logs file being opened during a period of time. Now I have exact these two functions in my 'filemon' utility:

set<wstring> wstrSet;

// callback - get notified by callback filter driver on any file open operation
void CbFltOpenFileN(
                                    CallbackFilter* Sender,
                                    LPWSTR FileName,
                                    ACCESS_MASK DesiredAccess,
                                    WORD FileAttributes,
                                    WORD ShareMode,
                                    DWORD CreateOptions,
                                    WORD CreateDisposition )
 {
      // don't log for directories
      if (FileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
          return;
      }
      wstring wstr = FileName;
      wstr.append(L"\n");
      //wstrSet.insert(wstr); // as soon as I un-comment this line I start getting BSOD in multiple execution of this utility
 }


// Read all paths stored in the set and write them into the log file
void WritePathsToLog() {
    typedef set<wstring>::const_iterator CI;
    printf("\nNo. of files touched ===> %d\n\n", wstrSet.size());
    for (CI iter = wstrSet.begin(); iter != wstrSet.end(); iter++) {
        fputws((*iter).c_str(), logFile);
    }
}

Basically what this code does is that 'filemon' utility interacts with callback filter driver and whenever file is touched by any process the driver reports the respective filename to the 'filemon' utility via CbFltOpenFileN function.

Now the issue is that this 'filemon' utility runs fine on win7 x64 machine 4GB machine but as soon as I uncomment the last line in the CbFltOpenFileN function i.e. start inserting the reported filename in the set then I start getting BSOD mostly with BugCheck 0xCC and sometimes with BugCheck 0x50 which basically indicates that "system is trying to access already freed memory"

P.S. on win7 x64 with 8GB RAM I am not seeing any issue at all irrespective of whether last line in the CbFltOpenFileN function is commented or not.

Currently, 'wstrSet' is using default allocator so do I need to use a specific allocator while declaring set wstrSet; if yes can someone tell me how & why?

  • Let me share some more information here:
    • I am using VS2010
    • My utility is a 32 bit application targeted for x86 platform
    • The fileystem filter driver I am using is callabck filter driver provided by Eldos corp.
    • Now I am tetsing this utility using a simulator which generates lots of files and then starts the 'filemon' utility, then it touches all those files and at the end stops the 'filemon' utility. This simulator repeates this process 25 times.
    • Now for case where last line is commented I get empty log file created 25 times as I don't insert anything in set but 'filemon' utility also doesn't causes any BSOD
    • But for case where last line is NOT commented I get log file created each time with path enteries as now I am inserting paths in the set but then during first few iteration, say 2nd or 3rd or 6th, itself 'filemon' utility hits this BSOD scenario.

It's hard for me to repro this issue in debug mode as my simulator take cares of the start/stop of 'filemon' utility and i need to run it multiple time to repro the issue.

Hope this added info helps!!!

Thanks and Regards,
Sachin

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不必你懂 2024-12-23 19:28:57

如果您的CbFltOpenFileN是一个回调函数,这是否意味着它有异步调用?在这种情况下,您可能需要使用互斥锁来保护全局变量 (wstrSet)...

If you CbFltOpenFileN is a callback function, does this means it has asynchron calls? in that case, you might want to protect the global variable (wstrSet) with a mutex...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文