3

I'm trying to use Python's ctypes to work with a DLL, but I occasionally run into a problem when I try to call a function that's passed as a pointer to another function.

A little background...I'm trying to build a userspace filesystem using Dokan (version 0.6.0). Somewhat loosely stated, Dokan is basically FUSE for Windows. I've wrapped the dokan header file using ctypes (similar to pydokan). That header file contains the definition for a function pointer that looks like this

typedef int (WINAPI *PFillFindData) (PWIN32_FIND_DATAW, PDOKAN_FILE_INFO);

It also contains the prototype of another function

int (DOKAN_CALLBACK *FindFilesWithPattern) (
     LPCWSTR,
     LPCWSTR,
     PFillFindData,
     PDOKAN_FILE_INFO);

The corresponding ctypes definitions look like this

PFillFindData = ctypes.WINFUNCTYPE(ctypes.c_int,
                                   PWIN32_FIND_DATAW, 
                                   PDOKAN_FILE_INFO)

FindFilesWithPattern = ctypes.WINFUNCTYPE(ctypes.c_int,
                                          ctypes.c_wchar_p,
                                          ctypes.c_wchar_p,
                                          PFillFindData,
                                          PDOKAN_FILE_INFO)

The implementation of this latter function (FindFilesWithPattern) must call the FillFindData function that it is passed. A basic implemenation looks like this

def FindFilesWithPattern(self,
                         FileName,
                         SearchPattern,
                         FillFindData,
                         DokanFileInfo):
    if FileName == '\\':
        File = WIN32_FIND_DATAW(FILE_ATTRIBUTE_NORMAL | FILE_ATTRIBUTE_READONLY, 
                                FILETIME(1, 1),
                                FILETIME(1, 1), 
                                FILETIME(1, 1), 
                                0, 
                                len(self.HelloWorldText), 
                                0, 
                                0, 
                                'Hello_World.txt',
                                'Hello_~1.txt')
        pFile = PWIN32_FIND_DATAW(File)
        FillFindData(pFile, DokanFileInfo)
        return 0
    else:
        return -ERROR_FILE_NOT_FOUND

When this function gets called, I occasionally get the following error:

Traceback (most recent call last):
    File "_ctypes/callbacks.c", line 313, in 'calling callback function'
    File "src/test.py", line 385, in FindFilesWithPattern
        FillFindData(pFile, DokanFileInfo)
WindowsError: exception: access violation reading 0x0000000000000008

I am vexed. At first glance this looks like I'm trying to access memory that's out of range. But this error only occurs occasionally. Sometimes everything works fine, and the results come back as expected. (To clarify, when I say occasionally, I mean that the error occurs on some runs of the program and not on others. The error seems to occur or not occur consistently within a single run.)

I wondered then if perhaps I was getting an error code back rather than a memory address. I found here that if this was an error code, then it might indicate "not enough memory". When I look at the system monitor, this doesn't seem to be a problem though. I've tried running various memory profilers like Heapy and Meliae, but neither of them seem to work with Python 2.7 on Windows 64-bit.

My next best guess was that this is an issue using a 64-bit OS. Perhaps the type being used for the function pointer isn't sufficient to properly address it. After doing some Googling, it seems that others have had issues with using ctypes in Win64. I've built my Dokan library for a 64-bit architecture. Is there a problem with my python code then?

Any help would be very much appreciated. I've struggled with this for some time now.

A similar post can be found here. It doesn't seem terribly analogous though.

Note: In the python code you'll see some types that aren't defined here (e.g. PDOKAN_FILE_INFO). Those are either structures or pointers to structures that I didn't include for the sake of brevity.

Community
  • 1
  • 1
Ryan
  • 4,517
  • 7
  • 30
  • 34
  • "The error seems to occur or not occur consistently within a single run." This suggests to me that the error is dependent on the input. So it is highly improbable that it is a memory issue; more likely a non-sanitized/unexpected input that your algorithm cannot handle. – Santa Jun 13 '11 at 17:12
  • I added a print statement to the FindFilesWithPattern function (print "function pointer: {}".format(PFillFindData)) as the first line of the function, and the error went away. When I comment out the print statement, the error comes back. Can anyone make sense of that? – Ryan Jun 13 '11 at 19:38
  • If I change the print statement in my comment above to something else (anything that doesn't include the .format(PFillFindData)), I get the error. – Ryan Jun 13 '11 at 19:49

1 Answers1

2

How do you instantiate and use your callback? A callback must remain in scope for the lifetime it may be called. See this answer. These


Edit

Clarifying what I think the issue is...

I see you have to implement a DOKAN_OPERATIONS structure that implements callbacks like FindFilesWithPattern, then call DokanMain with that structure to mount the filesystem. When you fill out the structure you should be creating a type for the callback, then instantiating the callback with a Python function. Something like (pseudocode):

#Create the callback pointer type
PFINDFILESWITHPATTERN = ctypes.WINFUNCTYPE(ctypes.c_int,
                                          ctypes.c_wchar_p,
                                          ctypes.c_wchar_p,
                                          PFillFindData,
                                          PDOKAN_FILE_INFO) 

# Implement in Python
def FindFilesWithPatternImpl(...):
    # implementation

# Create a callback pointer object
FindFilesWithPattern = PFINDFILESIWTHPATTERN(FindFilesWithPatternImpl)

# Create the required structure listing the callback
dokan_op = DOKAN_OPERATIONS(...,FindFilesWithPattern,...)

# Register the callbacks
DokanMain(byref(dokan_op))

You must keep a reference to object dokan_op for the lifetime the callback could be used. If you implement mounting Dokan similar to below:

def mount():
    # Create structure locally
    dokan_op = DOKAN_OPERATIONS(...)
    # Spin off thread to mount Dokan
    threading.Thread(DokanMain,args=(byref(dokan_op),))

mount()

Once mount() returns dokan_op will be destructed. This is the scenario described by my original answer and would cause the error you are seeing. I'm theorizing your problem since I don't know what the rest of the code looks like, but I think the way you've implemented FindFilesWithPattern in Python is correct, esp. since you say it works intermittently. I've had the failure you're seeing before and the above scenario or something like it will cause the error you're seeing.

Hope this helps...

Community
  • 1
  • 1
Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251
  • I probably shouldn't be calling it a callback in a sense. PFillFindData is really just a typedef. FindFilesWithPattern is called from the DLL by the operating system (after a "dir" command for example). When it is called, it passes a pointer to a function (FillFindData). The implementation for FillFindData actually exists in the DLL not the in my python code. I apologize if this is a bit confusing. – Ryan Jun 15 '11 at 16:45
  • Since PFillFindData is just a typedef for a particular function pointer, would I be better off just using c_void_p? – Ryan Jun 15 '11 at 16:50
  • UPDATE: I tried using c_void_p instead of PFillFindData, but it gave me an error (TypeError: 'long' object is not callable). – Ryan Jun 15 '11 at 16:59
  • So `FindFilesWithPattern` is a callback that you've implemented in Python that the operating system will call? And `PFillFindData` is a C function You need to call from the Python code in that callback? – Mark Tolonen Jun 16 '11 at 06:37
  • Thanks for looking into this more! Initially, I wasn't holding onto the DokanOperations structure like you mentioned. After your first post, I made it a member of a class that I then subclass for my actual file system. My code is structured very similarly to what you posted here. I'm not seeing the error right now so hopefully this is the solution. Thanks again! – Ryan Jun 16 '11 at 14:48