Thread: check the value return by struct s_client to a pointer not be zero?

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    11

    check the value return by struct s_client to a pointer not be zero?

    Hi guys ,
    i got segment fault , and when i trace , found it happens since the value of pointer which is returned by Struct S_client (*ptr) is zero
    Code:
    if (ptr !=0)
    i know , adding above line of code is not the solution and not correct for the case since above line only check for the pointer to the address of memory which is not expected for me, i would like to check the value of ptr which is returned in struct s_client , not be zero . so anyone know how could i do that , is appreciated?

    Here is my code :


    Code:
    int32_t chk_process (int32_t) {
    ...
    struct s_client *ptr = cur_client();
    
    //FIXME
    // how could i check in this line , just when the value of 
    // ptr is not zero , then it goes to it's next line?`
    send_data (ptr, index);
    ...
    ...
    }

    Code:
    struct s_client *cur_client(void){
        return (struct s_client *) pthread_getspecific(getclient);
    }
    Best Regards.
    Last edited by pooyair; 09-14-2012 at 09:36 AM.

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    'if (ptr != 0)' is the right test for an invalid return pointer from cur_client. so you would do
    Code:
    if (ptr != 0) {
       send_data(...)
    }
    else {
       // do something else or nothing
    }
    this implies your call to pthread_getspecific is failing. according to the man page, pthread_getspecific will return 0 if there is not enough memory (unlikely) or if the key, in your case 'getclient' is invalid.

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    11
    this implies your call to pthread_getspecific is failing. according to the man page, pthread_getspecific will return 0 if there is not enough memory (unlikely) or if the key, in your case 'getclient' is invalid.
    Yes , i think , pthread_getspecific is the case ...
    also in man page (qoute from here) :
    If pthread_getspecific is called on the key whose thread specific data is being destroyed, the value NULL is returned, unless pthread_setspecific was called previously on that key from within destr_function to set the value to non-NULL.
    Since ptr previously might be destroyed (so using if (ptr != 0) ) could not fix the problem , Does anyone know any solution for this problem , i don't know how with pthread_setspecific or any other trick could bypass this segmentation fault.


    @dmh20001
    the reason that i said ptr !=0 is not correct , since i tried with above codes , and still with the if statement i get zero vaule for ptr... (probably because pthread_getspecific , destroyed it before..
    Last edited by pooyair; 09-14-2012 at 10:07 AM.

  4. #4
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    523
    Quote Originally Posted by pooyair View Post
    Yes , i think , pthread_getspecific is the case ...
    also in man page (qoute from here) :


    Since ptr previously might be destroyed (so using if (ptr != 0) ) could not fix the problem , Does anyone know any solution for this problem , i don't know how with pthread_setspecific or any other trick could bypass this segmentation fault.


    @dmh20001
    the reason that i said ptr !=0 is not correct , since i tried with above codes , and still with the if statement i get zero vaule for ptr... (probably because pthread_getspecific , destroyed it before..
    pthread_getspecific doesn't destroy the key or value associated with that key. pthread_setspecific associates a value with a key (the key must have been previously obtained with pthread_key_create()). Where are you obtaining the key "getclient" and setting the value for "getclient"?

  5. #5
    Registered User
    Join Date
    Sep 2012
    Posts
    11
    Where are you obtaining the key "getclient" and setting the value for "getclient"?
    before obtaining pthread_getspecific ... Well I found a hacky fix to check if ptr is zero with usleep , this is not the best solution , but this workarround , prevent that segment fault so far.
    Anyway Thanks for Consideration.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 05-12-2011, 01:02 AM
  2. Replies: 1
    Last Post: 07-04-2007, 12:20 AM
  3. Check the return status of scanf
    By Taka in forum C Programming
    Replies: 10
    Last Post: 11-01-2006, 06:27 AM
  4. Weird struct pointer return
    By Devil Panther in forum C Programming
    Replies: 2
    Last Post: 07-24-2005, 12:05 PM
  5. Check the new operator's return value?
    By John.H in forum C++ Programming
    Replies: 2
    Last Post: 04-17-2003, 09:37 AM

Tags for this Thread