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

  • SEARCH
  • Home
  • 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 6359269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:29:45+00:00 2026-05-24T23:29:45+00:00

everyone. I’m having a memory allocation error using ctypes and a C code. I’m

  • 0

everyone. I’m having a memory allocation error using ctypes and a C code. I’m wondering if the memory problem is inside of C, or caused by an improper use of ctypes. The memory error is

python(79698) malloc: * error for object 0x15627ac08: incorrect checksum for freed object >- object was probably modified after being freed.
*
set a breakpoint in malloc_error_break to debug
Abort

or

python(76110,0x7fff70062ca0) malloc: * error for object 0x7d807e1078907df: pointer being >freed was not allocated
*
set a breakpoint in malloc_error_break to debug
Abort

depending on how I write the “free_some_memory” part of the code below. I won’t post a bunch of C code here, but assuming I’ve written the C code correctly, does my ctypes interface look correct?

Also, the failures I’m experiencing don’t always happen in the same place in my scripts, for a given version of “free_some_memory”. Is it possible that my C code memory management is written poorly such that Python memory management may or may not bungle up (de)allocations?

Dunno if this is important, but I’m working on a Mac, Snow Leopard.
Any help you can offer would be very appreciated.

Best,
jkmacc

My C code looks like:

/**** cfunction.c ****/
int cfun (double *y, char datafile[1024], int byteoffset, int num )
{
  allocate_some_memory;
  do_stuff;

  if ( error_condition )
  {
    free_some_memory;
    return ( -1 );
  }

  fill_y_here;
  free_some_memory;
  return ( 0 );
}

My ctypes wrapper looks like:

#pyfunction.py

import ctypes as C
import numpy as np

lib = C.CDLL('mylib.dylib')

def pyfun(DATAFILE, BYTEOFFSET, NUM):
    """
    DATAFILE: a string file name
    BYTEOFFSET: an integer
    NUM: an integer
    """

    #return integer success/failure flag
    lib.cfun.restype = C.c_int 

    #array memory to be filled inside of C
    Y = np.empty(NUM,dtype='double',order='C')

    cDATAFILE = C.create_string_buffer(DATAFILE,1024) 
    cBYTEOFFSET = C.c_int(int(BYTEOFFSET))
    cNUM = C.c_int(int(NUM))

    flag = lib.cfun(Y.ctypes.data_as(C.POINTER(C.c_double)), \ 
               cDATAFILE,cBYTEOFFSET,cNUM)

    if flag == -1:
        print("Something went wrong.")
        return -1
    else:
        return Y
  • 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-24T23:29:46+00:00Added an answer on May 24, 2026 at 11:29 pm

    I had this problem and here is what I figured out:

    If you map something with ctypes and then free it you will get an error when the ctypes object is destroyed. So, before you call free(), you need to make sure that there are no remaining references to it in the python code.

    So, your C code will be like:

    char* p;
    char* allocate() {
     p = asprintf("hello world");
     return(p)
    }
    void freep() {
     free(p);
    }
    

    And your Python code will be like:

    allocate = clib.allocate
    allocate.restype = c_char_p()
    p = allocate()
    print p
    # This is the key:
    del(p)
    clib.freep()
    

    If you want to see an error, just omit del(p)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hope everyone is doing well I am having a problem with make files in
everyone, i've a little problem how to use variable from another class file to
Everyone managing open-source-software runs into the problem, that with the time the process of
Everyone in my office uses Macs and therefore most use Safari. We have a
Everyone uses source-code control to manage versions (right?) and this provides some level of
everyone I got problem here I need to update a plist data in a
everyone, I have this piece of the code: void foo(int var, int var1) {
Greetings Everyone I am new in asp.net and i'm using RadControls for Asp.net Ajax
everyone. I'm trying to use an a href link to onclick toggle unhide and
Everyone remembers google browser sync right? I thought it was great. Unfortunately Google decided

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.