Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3612074
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T21:57:16+00:00 2026-05-18T21:57:16+00:00

When using Python CTypes there are the Structures, that allow you to clone c-structures

  • 0

When using Python CTypes there are the Structures, that allow you to clone c-structures on the Python side, and the POINTERS objects that create a sofisticated Python Object from a memory address value and can be used to pass objects by reference back and forth C code.

What I could not find on the documentation or elsewhere is what happens when a Python object containing a Structure class that was de-referenced from a returning pointer from C Code (that is – the C function alocated memory for the structure) is itself deleted. Is the memory for the original C structure freed? If not how to do it?

Furthermore — what if the Structure contains Pointers itself, to other data that was also allocated by the C function? Does the deletion of the Structure object frees the Pointers onits members? (I doubt so) Else – -how to do it? Trying to call the system “free” from Python for the Pointers in the Structure is crashing Python for me.

In other words, I have this structure filled up by a c Function call:

class PIX(ctypes.Structure):
    """Comments not generated
    """
    _fields_ = [
        ("w", ctypes.c_uint32),
        ("h", ctypes.c_uint32),
        ("d", ctypes.c_uint32),
        ("wpl", ctypes.c_uint32),
        ("refcount", ctypes.c_uint32),
        ("xres", ctypes.c_uint32),
        ("yres", ctypes.c_uint32),
        ("informat", ctypes.c_int32),
        ("text", ctypes.POINTER(ctypes.c_char)),
        ("colormap", ctypes.POINTER(PIXCOLORMAP)),
        ("data", ctypes.POINTER(ctypes.c_uint32))
    ]

And I want to free the memory it is using up from Python code.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T21:57:17+00:00Added an answer on May 18, 2026 at 9:57 pm

    The memory is not freed, because Python has no idea if or how it should be freed. Compare these two functions:

    void testfunc1(PIX *pix)
    {
        static char staticBuffer[256] = "static memory";
        pix->text = staticBuffer;
    }
    
    void testfunc2(PIX *pix)
    {
        pix->text = (char *)malloc(32);
        strcpy(pix->text, "dynamic memory");
    }
    

    Used like this:

    pix1, pix2 = PIX(), PIX()
    mylib.testfunc1(ctypes.byref(pix1))
    mylib.testfunc2(ctypes.byref(pix2))
    

    And then at some point, pix1 and pix2 go out of scope. When that happens, nothing happens to the inner pointers—if they pointed to dynamic memory (as is the case here with pix2 but not pix1), you are responsible for freeing it.

    The proper way to solve this problem is, if you allocate dynamic memory in your C code, you should provide a corresponding function that deallocates that memory. For example:

    void freepix(PIX *pix)
    {
        free(pix->text);
    }
    
    
    pix2 = PIX()
    mylib.testfunc2(ctypes.byref(pix2))
    ...
    mylib.freepix(ctypes.byref(pix2))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using python and CherryPy to create a simple internal website that about 2
I am working with some C code called from Python using ctypes. Somewhere in
I'm using ctypes to load a DLL in Python. This works great. Now we'd
Using Python, how would I go about reading in (be from a string, file
Using Python 2.6, is there a way to check if all the items of
Using Python's Imaging Library I want to create a PNG file. I would like
I want to start using Python for small projects but the fact that a
I have a third-party GUI program that I'm wrapping with a Python class (using
I'm trying to call io_submit using python ctypes. The code I'm writing is supposed
Using python 2.6.5, I can use the with statement without calling from __future__ import

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.