ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
This problem is related to a server-client program. My program that crashes is the client; the server is on the internet.
The client program seems to crash when I call gethostbyname the second time on the same ip address inside a child thread (where the first time called was in the main thread).
So here it is:
-main thread connects with a socket (and called gethostbyname along the way), keeps commmunicating.
-then it launches a child thread that has to connect to another socket and calls gethostbyname on the same ip address as parent thread. This is where it crashes.
The reason why gethostbyname gets called on the same ip address is because sometimes the server it needs to communicate with is the same one the parent thread was communicating with but this is not always true.
I used gdb to find out that it was crashing exactly at gethostbyname().
Hmm, the problem still existed with gethostbyname_r() so I worked around it and the repeated ip address does not get passed to gethostbyname again.
That seems to have got my program working.
I still wonder why gethostbyname was crashing on the second call on same ip address (even the reentrant one).
Thanks ta0kira for reminding me of using reentrant functions when working with pthreads.
I meant the ip address in string format, e.g. "127.0.0.1".
But I found the problem in my code, it is very embarrassing.
I overlooked this code too many times:
char *buffer = (char*)malloc(sizeof(100));
instead of:
char *buffer = (char*)malloc(100*sizeof(char));
This silly problem caused the program to crash whether I called free() or whether any system call called free().
It seems as gethostbyname() also calls free() (I guess after the first call), and there was another system call that crashed the program when it internally called free(); that made be suspicious of what might be really going on.
Everything seems to be working fine now.
Out of curiosity (and lack of knowledge), I am still not sure how calling this makes all subsequent free() calls anywhere else on other variables fails as well. In other words can someone explain what malloc exactly does when this is called:
Yes, sorry. Should have looked at the reference myself! I did a web search and it came up, so I thought my memory was correct, but didn't actually look at my code that uses it!
ta0kira
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.