c - 访问冲突执行位置 - GetFileAttributesA

标签 c winapi

我在 VS2012 上的 Windows 8.1 x64 上遇到 GetFileAttributedA 的特殊问题。函数运行良好,但偶尔语句会因

而崩溃

Unhandled exception at 0x00000003 in MyService.exe: 0xC0000005: Access violation executing location 0x00000003.

这是伪代码。

char    szItemAbsPath[MAX_PATH];
DWORD   rc = 0;
DWORD   dwFileAttribute = 0;

memset(szItemAbsPath, '\0', sizeof(szItemAbsPath));
sprintf(szItemAbsPath, "%s%s%s", "D:\\Root", FILE_SEPARATOR_STR, "temp.txt");

// check item type
dwFileAttribute = GetFileAttributesA(szItemAbsPath); //this is where program crashes
if (dwFileAttribute == INVALID_FILE_ATTRIBUTES)
{
    rc = GetLastError();
        return rc;
}

if (dwFileAttribute & FILE_ATTRIBUTE_DIRECTORY)
{
    // this is a dir
}

我基本上想检查给定的项目是文件还是文件夹。有时也会在 32 位构建中观察到此问题。

编辑:

迷你转储

Dump Summary
------------
Dump File:  MyDesktopService.dmp : C:\Users\anand\Desktop\MyDesktopService.dmp
Last Write Time:    09-04-2014 17:34:24
Process Name:   MyDesktopService.exe : D:\Works\My\My_Recovered_from_SVN_Crash\MyVS2012\NSLib\MyDesktopService.exe
Process Architecture:   x86
Exception Code: 0xC0000005
Exception Information:  The thread tried to read from or write to a virtual address for which it does not have the appropriate access.
Heap Information:   Present

System Information
------------------
OS Version: 6.2.9200
CLR Version(s): 

Modules
-------
Module Name Module Path Module Version
----------- ----------- --------------
MyDesktopService.exe    D:\Works\My\My_Recovered_from_SVN_Crash\MyVS2012\NSLib\MyDesktopService.exe 0.0.0.0
ntdll.dll   C:\Windows\System32\ntdll.dll   6.3.9600.16502
kernel32.dll    C:\Windows\System32\kernel32.dll    6.3.9600.16520
KERNELBASE.dll  C:\Windows\System32\KERNELBASE.dll  6.3.9600.16496
MSVCR110D.dll   D:\Works\My\My_Recovered_from_SVN_Crash\MyVS2012\NSLib\MSVCR110D.dll    11.0.51106.1
MyDesktop.dll   D:\Works\My\My_Recovered_from_SVN_Crash\MyVS2012\NSLib\MyDesktop.dll    0.0.0.0
libeay32.dll    D:\Works\My\My_Recovered_from_SVN_Crash\MyVS2012\NSLib\libeay32.dll 1.0.1.6
wtsapi32.dll    C:\Windows\System32\wtsapi32.dll    6.3.9600.16384
userenv.dll C:\Windows\System32\userenv.dll 6.3.9600.16384
advapi32.dll    C:\Windows\System32\advapi32.dll    6.3.9600.16384
ws2_32.dll  C:\Windows\System32\ws2_32.dll  6.3.9600.16384
libcurl_debug.dll   D:\Works\My\My_Recovered_from_SVN_Crash\MyVS2012\NSLib\libcurl_debug.dll    7.34.0.0
jansson.dll D:\Works\My\My_Recovered_from_SVN_Crash\MyVS2012\NSLib\jansson.dll  0.0.0.0
gdi32.dll   C:\Windows\System32\gdi32.dll   6.3.9600.16421
user32.dll  C:\Windows\System32\user32.dll  6.3.9600.16441
msvcr110.dll    C:\Windows\System32\msvcr110.dll    11.0.51106.1
msvcrt.dll  C:\Windows\System32\msvcrt.dll  7.0.9600.16384
rpcrt4.dll  C:\Windows\System32\rpcrt4.dll  6.3.9600.16384
profapi.dll C:\Windows\System32\profapi.dll 6.3.9600.16384
sechost.dll C:\Windows\System32\sechost.dll 6.3.9600.16384
nsi.dll C:\Windows\System32\nsi.dll 6.3.9600.16384
Wldap32.dll C:\Windows\System32\Wldap32.dll 6.3.9600.16384
normaliz.dll    C:\Windows\System32\normaliz.dll    6.3.9600.16384
ssleay32.dll    D:\Works\My\My_Recovered_from_SVN_Crash\MyVS2012\NSLib\ssleay32.dll 1.0.1.6
sspicli.dll C:\Windows\System32\sspicli.dll 6.3.9600.16408
CRYPTBASE.dll   C:\Windows\System32\CRYPTBASE.dll   6.3.9600.16384
bcryptPrimitives.dll    C:\Windows\System32\bcryptPrimitives.dll    6.3.9600.16384
winsta.dll  C:\Windows\System32\winsta.dll  6.3.9600.16384
IPHLPAPI.DLL    C:\Windows\System32\IPHLPAPI.DLL    6.3.9600.16384
winnsi.dll  C:\Windows\System32\winnsi.dll  6.3.9600.16384
dhcpcsvc6.DLL   C:\Windows\System32\dhcpcsvc6.DLL   6.3.9600.16384
dhcpcsvc.dll    C:\Windows\System32\dhcpcsvc.dll    6.3.9600.16384
mswsock.dll C:\Windows\System32\mswsock.dll 6.3.9600.16384
dnsapi.dll  C:\Windows\System32\dnsapi.dll  6.3.9600.16423
rasadhlp.dll    C:\Windows\System32\rasadhlp.dll    6.3.9600.16384
NapiNSP.dll C:\Windows\System32\NapiNSP.dll 6.3.9600.16384
pnrpnsp.dll C:\Windows\System32\pnrpnsp.dll 6.3.9600.16384
nlaapi.dll  C:\Windows\System32\nlaapi.dll  6.3.9600.16384
winrnr.dll  C:\Windows\System32\winrnr.dll  6.3.9600.16384
wshbth.dll  C:\Windows\System32\wshbth.dll  6.3.9600.16384
cryptsp.dll C:\Windows\System32\cryptsp.dll 6.3.9600.16384
rsaenh.dll  C:\Windows\System32\rsaenh.dll  6.3.9600.16384
bcrypt.dll  C:\Windows\System32\bcrypt.dll  6.3.9600.16384

调用堆栈:

    00000003()  Unknown
    [Frames below may be incorrect and/or missing]  
>   MyDesktop.dll!HandleModifyEvent(...) Line 159   C
    MyDesktop.dll!ProcessFSEvent(...) Line 79   C
    MyDesktop.dll!NSFileSysEventProcessor(void * lpParam=0x01adf324) Line 59    C
    MyDesktop.dll!thread_first_breath(sThrdInfo * psParam=0x009d2848) Line 1119 C
    msvcr110d.dll!_callthreadstartex() Line 354 C
    msvcr110d.dll!_threadstartex(void * ptd=0x009ff308) Line 337    C
    kernel32.dll!7790495d() Unknown
    ntdll.dll!77b498ee()    Unknown
    ntdll.dll!77b498c4()    Unknown

最佳答案

在合理的假设下,这段代码不会产生这个错误。这个数组的大小是 1 短,但它的长度仍然是 260,这对于示例来说已经足够了。

这种访问冲突强烈暗示存在疯狂的存储。函数的一个或多个指针参数已损坏,并在使用时触发访问冲突。

如果您可以使用最少的完整验证示例 (https://stackoverflow.com/help/mcve) 重现问题,那么将不会缺少帮助您找到错误的人。但到那时你会自己找到它。


正如评论中所指出的,“在位置 0x000000nn”的性质的访问冲突强烈表明对结构或类成员的 NULL 指针取消引用,该结构或类成员距对象开头有 nn 字节的偏移量。有时它会是一个指针,其中存储了一个小整数。有时它会是一个已递增的空值。所有这些都强烈暗示数据在某处被覆盖。

关于c - 访问冲突执行位置 - GetFileAttributesA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22961074/

相关文章:

c - 动态分配内存以保存用户输入

c - -Wcast-qual : cast discards ‘__attribute__((const))’ qualifier from pointer target type

c - C 中哈希码的递归

c++ - 回收站元素粉碎

c++ - 使用 CoInitialize 和 CoUninitialize 引发 C++ 异常

python - xlwings:如何获取单元格的值并指示消息框

c++ - win32编程子窗口没有退出按钮

c++ - 将函数指针作为 API 接口(interface)传递给已编译的库

c - 在同一个 makefile 中制作 C 和 ASM 文件失败

c++ - 多个对话过程来控制? winapi/C++