Memory leak using pywin32com for opc

2019-07-19 18:25发布

问题:

I am having a hard time trying to figure out how to address leaking memory. I think this might be an issue with pywin32 but I am not completely sure.

My code to do reading/writing individual items seems to work just fine, but when using group functions it slowly leaks memory. I suspect this is from the 1-based array that must be passed in the server_handles.

Does anyone know of a work around?

def read_group(self, group, mode=OPC_SYNC, source=OPC_DS_CACHE):
    """ Read a group returning a list of Result tuples or None. """
    if group not in self.groups:
        raise OPCError("Group does not exist, add first.")

    items = []
    server_handles = []
    for name, item in self.items.items():
        if item.group == group:
            items.append(name)
            server_handles.append(item.server_handle)

    if not server_handles:
        return None

    # Note that arrays are 1 based so must add an element to beginning
    server_handles.insert(0, 0)

    if mode == OPC_SYNC:
        val, err, qual, ts = self.groups[group].SyncRead(
                Source=source,
                NumItems=len(server_handles) - 1,
                ServerHandles=server_handles)

        # Map the return arrays into a list of result tuples
        result = []
        for i in range(len(items)):
            result.append(Result(
                    items[i],
                    val[i],
                    OPC_QUALITY[qual[i]],
                    str(ts[i]),
                    err[i],
                    ))

        return result