free() is useless... (I WAS WRONG)

M

Matt Gostick

It was my pthreads that were leaking.. not my string ops. doh. I
added pthread_join after my pthread_create and I have no more
problems. Perhaps this is not the correct group to ask, but from what
I understand pthread_join causes the main to 'sleep' until the pthread
returns. That's kinda pointless for me since I want to run _many_
pthreads at once. I looked into pthread_detach, but could not get it
to work properly. Should I be using pthread_join?

Matt

----------

Well, it's not useless, it just doesn't seem to be working for me.

Basically, when I'm returning a pointer from a function, free does not
seem to be freeing the memory for me. Here is an example of what I am
trying to do:

char *blah (void) {
char *string;

string = (char *) malloc(sizeof("foobar")*sizeof(char));
strcpy(string, "foobar");

return string;
}

int main (void) {
char *string;

string = blah();
printf("%s\n", string);
free(string);

return 0;
}


-----

I'm not quite sure how to properly diagnose this... as I haven't
touched C in quite some time. Although, the problem I am having now
is evident b/c the software is a high volume socket server and I can
see the process size growing with `top`.

Any help would be very much appretiated.
Thanks,
Matt.
 
A

Alex Fraser

Matt Gostick said:
It was my pthreads that were leaking.. not my string ops. doh. I
added pthread_join after my pthread_create and I have no more
problems.

Please reply in the thread. (I see you posted through Google, which might
explain things going by some other posts.)
Perhaps this is not the correct group to ask, but from what
I understand pthread_join [...]

No, it's not the right group to ask. Try comp.unix.programmer instead.

HTH,
Alex
 
K

Keith Thompson

It was my pthreads that were leaking.. not my string ops. doh. I
added pthread_join after my pthread_create and I have no more
problems. Perhaps this is not the correct group to ask, but from what
I understand pthread_join causes the main to 'sleep' until the pthread
returns. That's kinda pointless for me since I want to run _many_
pthreads at once. I looked into pthread_detach, but could not get it
to work properly. Should I be using pthread_join?

You're right; this isn't the correct group to ask. (Standard C has no
concept of threads.) Try comp.programming.threads.
 
D

Default User

Alex said:
Please reply in the thread. (I see you posted through Google, which
might explain things going by some other posts.)

The braindead google newsreader starts a new thread whenever the
subject is changed. There's really no good reason to ever use that as a
news service.

For those without news service from their ISP, the free service at
http://news.individual.net works fine.



Brian Rodenborn




Brian Rodenborn
 
R

Randy Howard

You're right; this isn't the correct group to ask. (Standard C has no
concept of threads.) Try comp.programming.threads.

Let me second this and explain why comp.programming.threads is a better
option than the often recommended comp.unix.programmer for this. Pthread
code (POSIX compliant) can be written on a fairly wide number of OS platforms
and compiler combinations, including Linux, Windows (with add-on library),
Mac OS X, Novell NetWare, and a slew of others. Also, several of the
"standard pedants" for pthreads similar to the C standard wordmeisters here
hang out there, although they tend to focus more on technical rather than
wordsmithing issues. Either way, although there is overlap, the answers
in c.p.t seem to be more generic whereas the answers in c.u.p often lean
towards platform-specific answers.
 
K

Kelsey Bjarnason

[snips]

It was my pthreads that were leaking.. not my string ops. doh.
string = (char *) malloc(sizeof("foobar")*sizeof(char));
strcpy(string, "foobar");

All other issues aside, why are you casting malloc, in direct violation of
about 78,000 posts in clc all saying don't do this, it's a bad idea?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,146
Messages
2,570,832
Members
47,374
Latest member
anuragag27

Latest Threads

Top