Check The value a pointer returned by struct s_client != 0

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

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 the c programming 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);
...
...
}
struct s_client *cur_client(void){
    return (struct s_client *) pthread_getspecific(getclient);
}

Best Regards.

---------- Post updated at 11:08 AM ---------- Previous update was at 10:39 AM ----------

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.

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..

There are many reasons it could be returning NULL, for instance, if it was never set in the first place, or used before the main thread managed to set it (race condition), or the main thread had a problem setting it(bug, out of memory), etc.

You can't simply "bypass" it. If it's checking for data there, it's looking for data the programmer was trying to provide it explicitly, and must need it. Investigate where the thread-specific data gets set in the first place, and see if there's ever a glitch in that -- check its return values, make sure the data being fed into it is sane, etc, etc.

I suppose for a really hacky fix, you could check if the data returned was NULL, wait a millisecond with usleep, and try again up to a maximum of 3 or 4 times, just in case the data hasn't been set yet, but that would reveal deeper fundamental issues with the program...

1 Like