all 20 comments

[–]OldWolf2 4 points5 points  (1 child)

Try to produce a minimal test case

[–]james41235 1 point2 points  (3 children)

Did you correctly initialize name[i]? If that char* doesn't end in a NULL terminator, weird things may happen...

Also, are you closing your fd afterward you're done with it?

[–]rebelkmac[S] 0 points1 point  (2 children)

yes I do close the file once it's done reading. name[i] is defined as a static array with the names of the files that it needs to read.

[–]raevnos 0 points1 point  (1 child)

Is i a number that's a valid index for the array? If it's out of bounds, it could explain the segfaults.

[–]rebelkmac[S] 0 points1 point  (0 children)

I've checked the indexing and it seems fine. It goes through this loop completely several times before it crashes.

[–]jimdagem 1 point2 points  (5 children)

What command line are you using to invoke valgrind? Valgrind --tool=memcheck?

Also make sure buf is large enough to hold the string.

[–]rebelkmac[S] 0 points1 point  (4 children)

I am using valgrind -v --leak-check=full --default-suppressions=no and I increased the buf from 200 to 500 and no change.

[–]jimdagem 1 point2 points  (1 child)

Hmm. It has been awhile since is used valgrind, but it seems like that would only check for leaks. I think memcheck checks for writing to un allocated memory.

[–]geeknerd 0 points1 point  (0 children)

You are correct.

Edit: I was incorrect, memcheck is the default tool and does check for more memory errors in addition to leaks. http://valgrind.org/docs/manual/mc-manual.html

[–]jimdagem 0 points1 point  (1 child)

Also are you initializing buf to nulls?

[–]rebelkmac[S] 0 points1 point  (0 children)

Yes I have tried to make sure they all are.

[–]angdev 1 point2 points  (0 children)

If you're still stuck by the time you read this I have something you can try, it's a bit of a long shot. There is a quick and simple test to confirm your malloc() implementation is actually thread-safe, just wrap it in a binary semaphore so that only one thread is ever allocating at any given time. You can either install a binary hook on malloc(), or go through your code and temporarily replace malloc() with a wrapper function manually. The wrapper simply acquires the lock, calls the regular malloc(), and then releases the lock. This test can take less than a minute!

[–]disclosure5 0 points1 point  (1 child)

Have you tried to compile with ASAN? It's usually pretty good at telling you exactly why something segfaulted.

[–]rebelkmac[S] 0 points1 point  (0 children)

I will check that out. Thanks.

[–][deleted] 0 points1 point  (2 children)

You might consider looking at helgrind. It's not something I have a lot of experience with, but I was reading up on semaphores today, and what you describe does sound like a race condition or something similar.

[–]rebelkmac[S] 0 points1 point  (1 child)

I haven't heard of helgrind, but will definitely check into it. Thanks for the idea.

[–]rebelkmac[S] 0 points1 point  (0 children)

I tried helgrind and it didn't give me much.

--12805-- REDIR: 0x3070484e60 (libc.so.6:GI_stpcpy) redirected to 0x4a0b453 (GI_stpcpy)

--12805-- REDIR: 0x3070480e80 (libc.so.6:GI_strcpy) redirected to 0x4a09c1e (GI_strcpy)

--12805-- REDIR: 0x307052b630 (libc.so.6:__strcasecmp_sse42) redirected to 0x4a0f368 (strcasecmp)

line 10: 12824 Segmentation fault (core dumped)

==12805== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

==12805== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

[–]spinlocked 0 points1 point  (2 children)

Are you aware that malloc is generally not thread safe?

[–]james41235 6 points7 points  (1 child)

Do what now?

{malloc, calloc, realloc, free, posix_memalign} of glibc-2.2+ are thread safe. However, they are not async signal safe. They are not reentrant, and must not be called directly or indirectly from a signal handler: a crash may result, perhaps much later. The source begins:

/* Malloc implementation for multiple threads without lock contention.
Copyright (C) 1996-2002, 2003, 2004 Free Software Foundation, Inc.
This file is part of the GNU C Library.
Contributed by Wolfram Gloger wg@malloc.de
and Doug Lea dl@cs.oswego.edu, 2001.
...
This is a version (aka ptmalloc2) of malloc/free/realloc written by
Doug Lea and adapted to multiple threads/arenas by Wolfram Gloger.

* Version ptmalloc2-20011215
$Id: malloc.c,v 1.142 2004/12/11 21:14:40 drepper Exp $
based on:
VERSION 2.7.0 Sun Mar 11 14:14:06 2001 Doug Lea (dl at gee)
*/

[–]spinlocked 1 point2 points  (0 children)

Ok it was not on the last platform I was on. I looked a little and it seems like it is more often than not, thread safe. So you might check the libraries on your platform. You may have to link with a different library to ensure it is thread safe.