LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 12-21-2010, 03:22 AM   #1
truboy
Member
 
Registered: Oct 2010
Location: Switzerland
Posts: 84

Rep: Reputation: 9
Terminated pthreads decreasing memory, until Seg Fault


Hi all,

I'm currently developping an app in C++ that has to transmit over CAN bus.
It runs on a SBC9261 board, under Linux 2.6.24.

Two pthreads are working for this : the first checks whether a CAN-frame was received. If so, it launches a second thread that has to handle the frame. This way, I'm able to receive frames while handling others (this is sometimes necessary, as some frame-processing needs informations from other CAN-devices).

Here is the code (a bit simplified) of the first thread's routine (the one which receive the frames) :

Code:
void *canRead(void *arg) {

    struct can_frame frameRecv;
    unsigned int i;
    pthread_attr_t attr;
    pthread_t threadHandler;

    // P. thread attribute
    pthread_attr_init(&attr);
    pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);

    while(1) {

        // Init frame
        memset(&frameRecv, 0, sizeof(struct can_frame));

        // Read from CAN
        read(canDevice, &frameRecv, sizeof(struct can_frame));

        // Threading to be able to receive while handling requests
        // pthread_create() prototype : int pthread_create(pthread *thread, const pthread_attr_t *attr, void *(*start_routine)(void*), void *arg_of_routine);
	if (pthread_create(&threadHandler, &attr, canHandler, &frameRecv)) {
            perror("pthread_create");
            exit(-1);
        }

        // Sleep for a while
        usleep(50*1000);
    }
}
canHandler is the routine that handles the CAN-frame. There is no infinite loop in it, and it could take like 3 secs to end, in the worst case.

This works fine, at least the first 15 minutes, because here is the problem :
The canHandler threads seem to remain in memory after they terminate, as it decreases until the program crashes, throwing a Segmentation Fault.

I spotted this behavior with the top command. If I remove the pthread_create line, the app can run all night without exceeding 17% of memory.

I took a look at the pthread_create manual and one gives a usage example where freeing memory is done.

The interessant part is :

Code:
void *res;

/*Creating different threads, doing stuff here...*/

pthread_join(<whatever_thread>, &res);

free(res);      /* Free memory allocated by thread */
But I can't use it that way, as I have no thread to join...

But does someone know how I could initialize that res pointer or use that free() function to stop those memory leaks ?

Thank you for your help or suggestions !
 
Old 01-04-2011, 08:17 AM   #2
truboy
Member
 
Registered: Oct 2010
Location: Switzerland
Posts: 84

Original Poster
Rep: Reputation: 9
I submitted my problem to a french forum and got this answer.

For those who don't speak French, here is the solution :

My threads were staying in memory because they were default joinable threads, which free the memory only when pthread_join() is executed.
What I needed were detached threads which release memory automatically after they terminate :

Code:
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
It works all right now !

I hope it can help !
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Seg Fault. Memory address not being passed to function. prushik Programming 4 04-21-2010 06:39 AM
Two threads somehow using the same data (seg fault with pthreads in c) fortenbt Programming 9 04-25-2008 12:34 PM
seg fault when strtok on static memory c++ gearoid_murphy Programming 5 04-30-2007 11:04 AM
seg. fault when allocating memory via a pointer inside a struct elmafiacs Programming 4 02-20-2005 07:26 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 04:09 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration