Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ComPtr] Automatically release all object in a array #57

Open
moi15moi opened this issue Sep 2, 2024 · 2 comments
Open

[ComPtr] Automatically release all object in a array #57

moi15moi opened this issue Sep 2, 2024 · 2 comments

Comments

@moi15moi
Copy link
Contributor

moi15moi commented Sep 2, 2024

How can I set the parameter own=True when I create a array with a ComPtr ?
My goal is to be sure to always release the object.

Currently, I do it like this, but it is clearly hacky:

def get_filepath_from_IDWriteFontFace(font_face: IDWriteFontFace) -> Set[str]:
    ...

    file_count = UInt32()
    hr = font_face.GetFiles(byref(file_count), None)
    if FAILED(hr):
        raise WinError(hr)

    font_files = (IDWriteFontFile * file_count.value)()
    hr = font_face.GetFiles(byref(file_count), font_files)
    if FAILED(hr):
        raise WinError(hr)

    for font_file in font_files:
            font_file._own = True
            ...
@ynkdir
Copy link
Owner

ynkdir commented Sep 3, 2024

I could not find a good solution fot it.

Array doesn't call __del__() for element.
And element of array can not have custom attribute like _own.

I think copying array to list is simple and explicit way for now.

font_files = [IDWriteFontFile(o.value, own=True) for o in font_files]

or we can create a list referring array memory.

def own_list(cls, size):
    def own(o):
        o._own = True
        return o
    return [own(o) for o in (cls * size)()]

...
font_files = own_list(IDWriteFontFile, file_count.value)
hr = font_face.GetFiles(byref(file_count), byref(font_files[0]))

@moi15moi
Copy link
Contributor Author

moi15moi commented Sep 7, 2024

comtypes (enthought/comtypes#612) use metaclass to release the object
Since ComPtr herit from c_void_p, I think we cannot use the same method, but it may gives you idea

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants