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

Use memory management functions #22

Draft
wants to merge 40 commits into
base: master
Choose a base branch
from
Draft

Use memory management functions #22

wants to merge 40 commits into from

Conversation

Fadila82
Copy link

@Fadila82 Fadila82 commented Jul 7, 2022

We were not freeing memory after use. This PR tries to fix this.

uplink_python/access.py Outdated Show resolved Hide resolved
uplink_python/access.py Outdated Show resolved Hide resolved
uplink_python/access.py Outdated Show resolved Hide resolved
uplink_python/access.py Outdated Show resolved Hide resolved
uplink_python/download.py Outdated Show resolved Hide resolved
raise _storj_exception(encryption_key_result.error.contents.code,
encryption_key_result.error.contents.message.decode("utf-8"))
return encryption_key_result.encryption_key
error_code = encryption_key_result.error.contents.code
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this is a pretty common pattern here. Also, though, it looks kind of fragile. Even if we are very careful this time, the next person to work on this code could easily mess something up.

It might be worth isolating this pattern to a few utility functions. Something like:

def unwrap_libuplink_result(result_object, finalizer, attribute_name):
    if bool(result_object.error):
        error_code = result_object.error.contents.code
        error_msg = result_object.error.contents.message.decode("utf-8")
        finalizer(result_object)
        raise _storj_exception(error_code, error_msg)
    result = getattr(result_object, attribute_name)
    finalizer(result_object)
    return result

def unwrap_encryption_key_result(result_object, uplink_handle):
    return unwrap_libuplink_result(result_object, uplink_handle.m_libuplink.uplink_free_encryption_key_result, 'encryption_key')

def unwrap_project_result(result_object, uplink_handle):
    return unwrap_libuplink_result(result_object, uplink_handle.m_libuplink.uplink_free_project_result, 'project')

# (and similar methods for StringResult, AccessResult, etc)

Then in this function you could replace this whole ending (everything from line 81 on) with:

    return unwrap_encryption_key_result(encryption_key_result, self.uplink)

The preexisting code in this repo is still pretty messy, though, so that could only help so much.

@Fadila82 Fadila82 changed the title Free unused struct Use memory management functions Nov 8, 2022
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

Successfully merging this pull request may close these issues.

2 participants