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 7736981
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T07:52:57+00:00 2026-06-01T07:52:57+00:00

I am sharing a set of globals between different threads, so I allocate the

  • 0

I am sharing a set of globals between different threads, so I allocate the memory as needed depending on how much is being placed into it. Kinda like a buffer. Whenever I try to free it from the same thread, but different function, glib goes crazy and segfaults.

I’m not a c programmer by far. I am trying to clear up memory leaks in the code that I hacked together.

char *keybuffer[], *valuesbuffer[], *returnbuffer[], *bufferaction;
int memcache_lock=1,*bufferelements, bufferowner=0;


void memcache_clear_buffers(){   
        free(&bufferaction); //I get the same reaction when I have the & and without the &
//      free(*keybuffer);    //I thought maybe I need to use the pointer. Then
//      free(*valuesbuffer); // I thought maybe I needed to reference the address in memory.
//      free(*returnbuffer); // I appreciate any help.
//      free(*bufferelements);
}

void memcache_allocate_buffers(int size){
        *keybuffer = (char *)malloc(size * sizeof(char *));
        *valuesbuffer = (char *)malloc(size * sizeof(char *)); 
        *returnbuffer = (char *)malloc(size  * sizeof(char *));
        bufferelements = malloc(sizeof(int));
        bufferaction = (char *)malloc(sizeof(char*));
}

Here is the output from valgrind.

 3500== Invalid free() / delete / delete[]
 ==3500==    at 0x4027C02: free (vg_replace_malloc.c:366)
 ==3500==    by 0x4191D4C: memcache_clear_buffers (dm_memcache.c:254)
 ==3500==    by 0x41921ED: memcache_set (dm_memcache.c:328)
 ==3500==    by 0x4192281: memcache_sid (dm_memcache.c:151) 
 ==3500==    by 0x418316D: db_user_exists (dm_db.c:3208)
 ==3500==    by 0x403F7F2: auth_user_exists (authsql.c:49)
 ==3500==    by 0x419ADA6: auth_user_exists (authmodule.c:130)
 ==3500==    by 0x4040D4E: auth_validate (authsql.c:333)
 ==3500==    by 0x419B106: auth_validate (authmodule.c:163)
 ==3500==    by 0x74656CFF: ???
 ==3500==  Address 0x41b55c0 is 0 bytes inside data symbol "bufferaction"

How is it an invalid free?

  • 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-06-01T07:52:58+00:00Added an answer on June 1, 2026 at 7:52 am
    • malloc() and free() are not thread-safe functions. You need to protect the calls to those functions with a mutex.
    • You need to protect all shared variables with a mutex as well. You can use the same one as you use for malloc/free, one per variable.
    • You need to declare variables shared between several threads as volatile, to prevent dangerous optimizer bugs on some compilers. Note that this is no replacement for mutex guards.
    • Are the buffers arrays, or two-dimensional arrays (like arrays of C strings)? You have declared all buffers as potential two-dimensional arrays, but you never allocate the inner-most dimension.
    • Never typecast the result of malloc in C. Read this and this.
    • free(bufferaction), not free(&bufferaction).
    • Initialize all pointers to NULL explicitly. After free(), make sure to set the pointer to NULL. Before the memory is accessed by either thread, make sure to check the pointer against NULL.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've used Clint Harris' tutorial to set up code sharing between projects, and everything
What is a good pattern for sharing data between related views?. I have an
I am trying to set up simple Cross-Origin Resource Sharing using jQuery (1.7.1) powered
I have a set of iFrames, actually social sharing buttons from Facebook, Google, etc.,
public with sharing class xml4 { public string x{get;set;} public string c{get;set;} List<ArtemisVC__Company_View_Extensions__c> mcs{get;
I am sharing contract between my client and server (not using generated proxies). Here
I'm creating a music sharing site, so each user can set up his account,
I've got a project I'd like to start sharing on github which I'd set
I want to set sharing rights of many folders by using a Python script
I've set up Cross-Origin Resource Sharing on a server (Jetty using the CrossOriginFilter) and

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.