Click here to Skip to main content
       

C / C++ / MFC

 
QuestionRe: Fix: Ribbon text displays inactive after migrating to VS2012 PinmvpRichard MacCutchan25-Feb-13 21:49 
QuestionMemory leak Pinmembera_matseevsky25-Feb-13 9:19 
AnswerRe: Memory leak PinmemberRichard Andrew x6425-Feb-13 10:11 
GeneralRe: Memory leak Pinmembera_matseevsky25-Feb-13 16:07 
GeneralRe: Memory leak PinmemberStephen Hewitt25-Feb-13 16:43 
GeneralRe: Memory leak PinmemberRichard Andrew x6426-Feb-13 9:11 
AnswerRe: Memory leak PinmemberDavidCrow25-Feb-13 13:24 
AnswerRe: Memory leak PinmemberStefan_Lang26-Feb-13 4:04 
GeneralRe: Memory leak Pinmembera_matseevsky26-Feb-13 10:59 
GeneralRe: Memory leak PinmemberStefan_Lang26-Feb-13 22:44 
GeneralRe: Memory leak Pinmembera_matseevsky27-Feb-13 2:09 
GeneralRe: Memory leak [modified] PinmemberStefan_Lang27-Feb-13 2:52 
GeneralRe: Memory leak Pinmembera_matseevsky27-Feb-13 11:44 
Question[Solved] Memory leak in producer-consumer program [modified] Pinmembernoislude25-Feb-13 7:30 
Hi all. Nice to meet you. Could you tell me how can I fix the memory leak of this code? I don't have a clue. Thanks.
 
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>

 
#define MAX 100
#define BUFSZ 10
 
int i;
int buffindex = 0;
char* buffer;
 
void* producer(void* p);
void* consumer(void* p);
 
pthread_cond_t cond_prod = PTHREAD_COND_INITIALIZER;
pthread_cond_t cond_con = PTHREAD_COND_INITIALIZER;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
 
int main(void)
{
    pthread_t prod, con;
    
    if((buffer = malloc(sizeof(char) * BUFSZ)) == NULL)        
    {     
       perror("malloc");
       exit(EXIT_FAILURE);
    }
    
    pthread_mutex_init(&mutex, NULL);
    pthread_cond_init(&cond_prod, NULL);
    pthread_cond_init(&cond_con, NULL);
    pthread_create(&prod, NULL , producer, NULL);
    pthread_create(&con,  NULL , consumer, NULL);
    pthread_join(prod, NULL);
    pthread_join(con,  NULL);
    pthread_cond_destroy(&cond_prod);
    pthread_cond_destroy(&cond_con);
    pthread_mutex_destroy(&mutex);
    free(buffer);
    return EXIT_SUCCESS;
}
 
void* producer(void* p)
{
    for(i = 0; i < MAX; i++)
    {
       pthread_mutex_lock(&mutex); 
       if(buffindex == BUFSZ)
       {
          pthread_cond_wait(&cond_prod, &mutex); /* down */
       }    
       buffer[buffindex++] = '@';
       printf("Produce: %d\n", buffindex);
       pthread_cond_signal(&cond_con); /* up */
       pthread_mutex_unlock(&mutex);
    }    
    pthread_exit(EXIT_SUCCESS);
    return NULL;
}
 
void* consumer(void* p)
{
   for(i = 0; i < MAX; i++) 
   {
      pthread_mutex_lock(&mutex);
      if(buffindex == -1)
      {
         pthread_cond_wait(&cond_con, &mutex); /* down */
      }     
      printf("Consume: %d\n", buffindex--);
      buffer[buffindex] = '\0';
      pthread_cond_signal(&cond_prod); /* up */ 
      pthread_mutex_unlock(&mutex);
   }    
   pthread_exit(EXIT_SUCCESS);
   return NULL;
}


modified 27-Feb-13 15:48pm.

AnswerRe: Memory leak in producer-consumer program PinmemberRichard Andrew x6425-Feb-13 10:08 
GeneralRe: Memory leak in producer-consumer program Pinmembernoislude25-Feb-13 10:23 
GeneralRe: Memory leak in producer-consumer program PinmemberRichard Andrew x6425-Feb-13 10:33 
GeneralRe: Memory leak in producer-consumer program Pinmembernoislude25-Feb-13 10:42 
QuestionRe: Memory leak in producer-consumer program PinmemberDavidCrow25-Feb-13 13:28 
AnswerRe: Memory leak in producer-consumer program Pinmembernoislude25-Feb-13 13:33 
GeneralRe: Memory leak in producer-consumer program PinmemberStefan_Lang26-Feb-13 3:29 
GeneralRe: Memory leak in producer-consumer program Pinmembernoislude26-Feb-13 8:07 
GeneralRe: Memory leak in producer-consumer program PinmemberStefan_Lang26-Feb-13 22:21 
GeneralRe: Memory leak in producer-consumer program Pinmembernoislude26-Feb-13 22:27 
GeneralRe: Memory leak in producer-consumer program PinmemberStefan_Lang26-Feb-13 23:01 
SuggestionRe: Memory leak in producer-consumer program Pinmembernoislude27-Feb-13 9:47 
AnswerRe: Memory leak in producer-consumer program PinmemberVaclav_Sal25-Feb-13 17:45 
QuestionWhat is the MFC future PinmemberArris7425-Feb-13 7:06 
AnswerRe: What is the MFC future PinmemberVaclav_Sal25-Feb-13 9:46 
GeneralRe: What is the MFC future PinmemberRichard Andrew x6425-Feb-13 10:07 
AnswerRe: What is the MFC future Pingroupyu-jian25-Feb-13 21:30 
GeneralRe: What is the MFC future PinprotectorPete O'Hanlon26-Feb-13 3:30 
AnswerRe: What is the MFC future PinmemberStefan_Lang26-Feb-13 3:16 
GeneralRe: What is the MFC future Pingroupyu-jian26-Feb-13 17:44 
AnswerRe: What is the MFC future Pingroup_AnsHUMAN_ 27-Feb-13 1:42 
AnswerRe: What is the MFC future PinmemberJoe Woodbury4-Mar-13 12:00 
QuestionWere is a source of "sal.h" header file? PinmemberVaclav_Sal25-Feb-13 5:19 
AnswerRe: Were is a source of "sal.h" header file? PinmvpRichard MacCutchan25-Feb-13 5:44 
GeneralRe: Were is a source of "sal.h" header file? PinmemberVaclav_Sal25-Feb-13 5:52 
GeneralRe: Were is a source of "sal.h" header file? PinmemberSoMad25-Feb-13 6:39 
GeneralRe: Were is a source of "sal.h" header file? PinmemberVaclav_Sal25-Feb-13 9:21 
QuestionGoogle contacts from Visual Studio 2008 C++ desktop application PinmemberMember 86892625-Feb-13 0:54 
QuestionHow to resize a property sheet according to the DPI settings in MFC. Pinmembermbatra3124-Feb-13 22:35 
AnswerRe: How to resize a property sheet according to the DPI settings in MFC. PinmvpRichard MacCutchan25-Feb-13 0:22 
QuestionDisplaying Image from stream Pinmembertoms from newdelhi24-Feb-13 17:55 
AnswerRe: Displaying Image from stream PinmemberSoMad24-Feb-13 18:58 
GeneralRe: Displaying Image from stream Pinmembertoms from newdelhi24-Feb-13 19:05 
GeneralRe: Displaying Image from stream PinmemberSoMad24-Feb-13 19:10 
GeneralRe: Displaying Image from stream Pinmembertoms from newdelhi24-Feb-13 19:53 
GeneralRe: Displaying Image from stream PinmemberSoMad24-Feb-13 20:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.


Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 18 Sep 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid