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


 
Thread Tools Search this Thread
Top Forums Programming Check The value a pointer returned by struct s_client != 0
# 1  
Old 09-14-2012
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
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 the c programming 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.

---------- 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) :
Quote:
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.



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..
# 2  
Old 09-14-2012
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...

Last edited by Corona688; 09-14-2012 at 02:05 PM..
This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Get pointer for existing device class (struct class) in Linux kernel module

Hi all! I am trying to register a device in an existing device class, but I am having trouble getting the pointer to an existing class. I can create a class in a module, get the pointer to it and then use it to register the device with: *cl = class_create(THIS_MODULE, className);... (0 Replies)
Discussion started by: hdaniel@ualg.pt
0 Replies

2. Programming

Declare member of struct as a pointer in c

I have a uint8_t *C = malloc(24*sizeof(uint8_t)); I need to send some integers and this *C to another node(in ad hoc network). So I am going to use a struct ` struct fulMsg { int msgType; int msgCount; //uint8_t *CC; } fulMsg_t; typedef struct fulMsg fulMsg_tt;` there is a method... (1 Reply)
Discussion started by: chap
1 Replies

3. Shell Programming and Scripting

check variable value when nothing is returned

Hi all, I am wondering how can I check when a variable has nothing returned in it. I am trying to store a pid in this variable to see if a script is running in the background. So I am using something like that proc_pid=`ps -ef |grep script.sh|grep -v grep|awk '{print $2}'` if then ... (1 Reply)
Discussion started by: geovas
1 Replies

4. Programming

Storing C++-struct in file - problem when adding new item in struct

Hi, I have received an application that stores some properties in a file. The existing struct looks like this: struct TData { UINT uSizeIncludingStrings; // copy of Telnet data struct UINT uSize; // basic properties: TCHAR szHost; //defined in Sshconfig UINT iPortNr; TCHAR... (2 Replies)
Discussion started by: Powerponken
2 Replies

5. Programming

how to check if something exists in a struct linked list?

can someone provide an example of a struct linked list, where it has strings as its values, and then how do I check if a specific string (say called buffer) exists in the list of structs? i dont understand how to make a copy of it to check with this is what i have ... (0 Replies)
Discussion started by: omega666
0 Replies

6. Programming

Dereferencing pointer to a shared memory struct

I have what should be a relatively simple program (fadec.c) that maps a struct from an included header file (fadec.h) to a shared memory region, but I’m struggling accessing members in the struct from the pointer returned by shmat. Ultimately, I want to access members in the shared memory structure... (2 Replies)
Discussion started by: arette
2 Replies

7. Programming

linked list node with pointer to struct

Suppose to have: struct Tstudent { string name, surname; int matriculation_num; }; struct Tnode { Tstudent* student; Tnodo* next; } L;I want to deference that "student" pointer. For example, I tried with: *(L->student).matriculation_numbut it not worked, as terminal... (4 Replies)
Discussion started by: Luke Bonham
4 Replies

8. Shell Programming and Scripting

How to use s_client command

HI, Am not able to close open ssl session once after excuting the s_client command.Please help me. Find below command which am using in my script: ===================================== openssl s_client -connect localhost:443 -debug >/tmp/tempfile.txt 2>&1 after this can you please... (2 Replies)
Discussion started by: sparks
2 Replies

9. Programming

pass a pointer-to-pointer, or return a pointer?

If one wants to get a start address of a array or a string or a block of memory via a function, there are at least two methods to achieve it: (1) one is to pass a pointer-to-pointer parameter, like: int my_malloc(int size, char **pmem) { *pmem=(char *)malloc(size); if(*pmem==NULL)... (11 Replies)
Discussion started by: aaronwong
11 Replies

10. Programming

Pointer to a struct (with pointers) *** glibc detected *** double free

I am using a structure defined as follows struct gene_square { double *x; double *y; };I have class, with a member function which is a pointer of this type: gene_square* m_Genes;I am allocating memory in the constructors like this: m_Genes = new gene_square; for (ii=0;... (1 Reply)
Discussion started by: jatoo
1 Replies
Login or Register to Ask a Question