Python: WindowsError: exception: access violation reading 0x00000000

老子叫甜甜 提交于 2019-12-12 06:10:10

问题


This short function is simply getting the available free space of a storage device, however I am getting the above error when running the code.

The function is:

def disk_space1(drive):
    freespace = ctypes.c_ulonglong()
    calcspace = ctypes.windll.kernel32.GetDiskFreeSpaceExA
    calcspace(drive, ctypes.byref(freespace))
    disk_size = freespace.value
    return disk_size

This function worked perfectly until today when it has stopped working for no reason, I haven't changed anything. What's baffling me the most is that the function works properly, if I print out the value of 'freespace' once running it, it has gone and found the correct value, but still gives the error.

What could have caused this issue?


回答1:


You are not calling the full function signature which I believe is leading to access violations (due to random memory writes) and errors. The full function signature is (fully documented here):

BOOL WINAPI GetDiskFreeSpaceEx(
  _In_opt_   LPCTSTR lpDirectoryName,
  _Out_opt_  PULARGE_INTEGER lpFreeBytesAvailable,
  _Out_opt_  PULARGE_INTEGER lpTotalNumberOfBytes,
  _Out_opt_  PULARGE_INTEGER lpTotalNumberOfFreeBytes
);

By changing the function to:

def disk_space(drive):
    freespace = ctypes.c_ulonglong()
    calcspace = ctypes.windll.kernel32.GetDiskFreeSpaceExA
    err = calcspace(drive,
                    ctypes.byref(freespace),
                    None,
                    None)
    assert err != 0, 'calcspace failed'
    disk_size = freespace.value
    return disk_size

I was able to run it without intermittent error.



来源:https://stackoverflow.com/questions/28631078/python-windowserror-exception-access-violation-reading-0x00000000

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!