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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:49:52+00:00 2026-05-24T09:49:52+00:00

Very confused over this. I’ve just started learning about pointers and have now decided

  • 0

Very confused over this. I’ve just started learning about pointers and have now decided to go a bit deeper and start experimenting with threads. What I’m trying to do is pass a pointer onto a thread, so (after mallocing data) I call:

pthread_create(&ptThread, &ptAttr, newClient, (void *) data);

Inside the newClient function, I do what I need to do, and I decided (as there was a pointer allocated) to free it:

void *newClient(void *v) {
    // ...stuff happens here...

    free(v);
}

At the free(v); part, I get a Segmentation fault. So I used valgrind, and I got this saying it was an invalid free:

==1214== Invalid free() / delete / delete[]
==1214==    at 0x4023B6A: free (vg_replace_malloc.c:366)
==1214==    by 0x804F622: newClient (xxxxxx2.c:44)
==1214==    by 0x4032954: start_thread (pthread_create.c:300)
==1214==    by 0x4112E7D: clone (clone.S:130)
==1214==  Address 0x4 is not stack'd, malloc'd or (recently) free'd

To add to the fire, as I quit my program, valgrind told me I had a memory leak because v wasn’t free‘d:

==1214== 8 bytes in 2 blocks are definitely lost in loss record 1 of 1
==1214==    at 0x4023F50: malloc (vg_replace_malloc.c:236)
==1214==    by 0x804F453: main (xxxxxx1.c:94)

My questions – how is it an invalid free? Is there something I’m missing here? Do I need to free(v);?

  • 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-24T09:49:52+00:00Added an answer on May 24, 2026 at 9:49 am

    First of all, 0x4 doesn’t look like a valid address.

    ==1214==  Address 0x4 is not stack'd, malloc'd or (recently) free'd
    

    What I think is happening is that you are accidentally changing v. This may be because of a careless memcpy / strcpy overwriting the stack of newClient.

    On another note, generally it is recommended that both malloc and free are done in the same thread. I heard some allocators are designed to do it more efficiently that way.

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

Sidebar

Related Questions

So, I am very confused over a quick test that I just ran. I
I may sound a little childish but i am bit confused over a very
You guys were very helpful yesterday. I am still a bit confused here though.
I'm fairly new to HTTPS/SSL/TLS and I'm a bit confused over what exactly the
I am very confused over something and was wondering if someone could explain. In
I am very confused over the time at which memory is allocated to Java
I'm very confused about how to get TTS working in VB 2010 Express. I
I am very confused about the following: echo(<a href='http://.urlencode(www.test.com/test.php?x=1&y=2).'>test</a><br>); echo(<a href='http://.www.test.com/test.php?x=1&y=2.'>test</a>); The first link
I guess this has been asked before here , but I'm still confused about
Hey everybody, I'm very confused about how to use two different Cell Types in

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.