Thread: fine with VALGRIND, but not working without it

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    114

    fine with VALGRIND, but not working without it

    Hi everyone,

    I don't know how many of you use valgrind to check on memory leaks and memory allocation problems.

    What is happening with my code is strange in two ways:

    1) the code was perfectly working until one day ago, but suddenly it is now giving problems on memory in Linux Ubuntu

    1b) When however I execute the code using valgrind, the problem seems to disappear and it executes again until exit.

    2) This same code, however, does not run into any type of problem when compiled and executed on a Mac OSX Darwin

    Is anyone able to help me on this? What type of info would you need from me and from the code?

    thank you very much in advance,
    cfd

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    You could explain what you mean by "giving problems" but I will assume it is a seg fault, and that is because you have an overflow somewhere.

    Overflows may or may not cause a problem, it depends on what they are next to in memory. This is determined at compile time, meaning you can compile code on one compiler and get away with an error that you don't get away with on another compiler.

    I don't know much about valgrind or it's internals but since it does monitor memory addresses it could be remapping them which might again conceal the error.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    114
    Hi MK27, thanks for replying.

    yes, it is a segmentation fault, acuatlly, this is exactly wht I get:

    Code:
    *** glibc detected *** ./meteoIni.a: realloc(): invalid next size: 0x0805bd68 ***
    ======= Backtrace: =========
    /lib/tls/i686/cmov/libc.so.6[0xb7e22803]
    /lib/tls/i686/cmov/libc.so.6(realloc+0x10b)[0xb7e2475b]
    /lib/tls/i686/cmov/libc.so.6(realloc+0x3c)[0xb7e2468c]
    ./meteoIni.a[0x8055975]
    ./meteoIni.a[0x80493d4]
    /lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xe0)[0xb7dcb450]
    ./meteoIni.a[0x8048aa1]
    ======= Memory map: ========
    08048000-0805a000 r-xp 00000000 08:02 2605613    /home/simone/Documents/scientific/CFD/Alya_initialization/MeteoIni_Dev/Source/meteoIni.a
    0805a000-0805b000 rw-p 00011000 08:02 2605613    /home/simone/Documents/scientific/CFD/Alya_initialization/MeteoIni_Dev/Source/meteoIni.a
    0805b000-0807c000 rw-p 0805b000 00:00 0          [heap]
    b7c00000-b7c21000 rw-p b7c00000 00:00 0 
    b7c21000-b7d00000 ---p b7c21000 00:00 0 
    b7db4000-b7db5000 rw-p b7db4000 00:00 0 
    b7db5000-b7efe000 r-xp 00000000 08:02 2293784    /lib/tls/i686/cmov/libc-2.7.so
    b7efe000-b7eff000 r--p 00149000 08:02 2293784    /lib/tls/i686/cmov/libc-2.7.so
    b7eff000-b7f01000 rw-p 0014a000 08:02 2293784    /lib/tls/i686/cmov/libc-2.7.so
    b7f01000-b7f04000 rw-p b7f01000 00:00 0 
    b7f04000-b7f27000 r-xp 00000000 08:02 2293788    /lib/tls/i686/cmov/libm-2.7.so
    b7f27000-b7f29000 rw-p 00023000 08:02 2293788    /lib/tls/i686/cmov/libm-2.7.so
    b7f34000-b7f3e000 r-xp 00000000 08:02 2294001    /lib/libgcc_s.so.1
    b7f3e000-b7f3f000 rw-p 0000a000 08:02 2294001    /lib/libgcc_s.so.1
    b7f3f000-b7f42000 rw-p b7f3f000 00:00 0 
    b7f42000-b7f43000 r-xp b7f42000 00:00 0          [vdso]
    b7f43000-b7f5d000 r-xp 00000000 08:02 2295295    /lib/ld-2.7.so
    b7f5d000-b7f5f000 rw-p 00019000 08:02 2295295    /lib/ld-2.7.so
    bfab2000-bfac7000 rw-p bffeb000 00:00 0          [stack]
    Aborted
    thank you again

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    realloc(): invalid next size

    Maybe the pointer involved had part of it's address overwritten? But someone else can probably interpret that error better than me.

    If it is an overwrite, check your global assignments first and then consider the "call stack" containing realloc(), that is, the function that calls realloc() and the sequence of functions that call that function, which somewhere that stack could have the pointer declared on it and another variable that overflows.

    You could post the code of that function too.
    Last edited by MK27; 09-03-2009 at 12:08 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Well, you're probably going to have to provide some code here. Obviously it looks like the realloc is failing, perhaps because the heap got fragmented or you ran out of memory?

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by cfdprogrammer View Post
    *** glibc detected *** ./meteoIni.a: realloc(): invalid next size: 0x0805bd68 ***
    So, the value 0x0805bd68 looks like a pointer. Do you have a dynamic array somewhere which is an array of pointers? You are probably overflowing that array.

    This is the sort of thing that Valgrind is supposed to catch. How are you compiling your program in the failing case? How are you compiling it to run through Valgrind? Are you using different switches in each case? And how exactly are you invoking Valgrind?
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  7. #7
    Registered User
    Join Date
    Mar 2009
    Posts
    114
    Quote Originally Posted by rags_to_riches View Post
    Well, you're probably going to have to provide some code here. Obviously it looks like the realloc is failing, perhaps because the heap got fragmented or you ran out of memory?
    Hi, thanks for replying. I may indeed be running otu of memory since this is not happening on another machine with twice as much RAM. What is strange though, is that it does not "fail" when larger arrays are used throughout the same code. It fails with a small arrays only considering the array as variables taken from a proper input file.

  8. #8
    Registered User
    Join Date
    Mar 2009
    Posts
    114
    Quote Originally Posted by brewbuck View Post
    So, the value 0x0805bd68 looks like a pointer. Do you have a dynamic array somewhere which is an array of pointers? You are probably overflowing that array.
    I actually have an array of pointers. I use two and are structured as arrays of strings dynamically allocated. This is good start because I actually modified one of them recently. I'll check on that tomorrow to begin with. Thanks for the hint.

    This is the sort of thing that Valgrind is supposed to catch. How are you compiling your program in the failing case? How are you compiling it to run through Valgrind? Are you using different switches in each case? And how exactly are you invoking Valgrind?
    I compile my code with the following makefile (See below), and invoke valgrind in its simplest way:

    Code:
    valgrind ./myexe.a [input_arguments]
    Code:
    # Makefile for the code PanelMethod.c
    
    # Varialble Definitions and Flags:
    
    CC = gcc     #Compiler is gcc
    LD = gcc -lm #Linker is gcc also
    
    CFALGS = -Wall -shared
    CDEBUG = -g
    
    #CBLAS = -lgsl -lgslcblas
    
    # Linker flags go here. Currently there aren't any, but if we'll switch to
    # code optimization, we might add "-s" here to strip debug info and symbols.
    LDFLAGS =
    
    # List of source codes and generated object files:
    SRCS = main.c READ_INPUT.c READ_MESH.c INIT.c STATE.c ANOMALY.c wrt2initf.c wrt2plotfile.c nrutil.c 
    OBJS = main.o READ_INPUT.o READ_MESH.o INIT.o STATE.o ANOMALY.o wrt2initf.o wrt2plotfile.o nrutil.o
    
    # ProgRam executable file name:
    EXE = meteoIni.a
    
    # Top-level rule, to compile everything
    all: $(EXE)
    
    # Rule to Link the program
    $(EXE): $(OBJS)
            $(LD) -L/usr/local/lib $(OBJS) $(GSLLIB) $(CBLAS) -o $(EXE)
    
    # Rule for all files:
    $(OBJS): $(SRCS)
            $(CC) $(CFLAGS) $(CDEBUG) -I/usr/include -c $(SRCS)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 05-07-2009, 11:31 AM
  2. erors from Valgrind ,,
    By prakash0104 in forum C Programming
    Replies: 2
    Last Post: 04-05-2009, 07:33 PM
  3. Bug hunting with Valgrind
    By Smjert in forum C Programming
    Replies: 8
    Last Post: 11-06-2008, 04:21 AM
  4. Replies: 1
    Last Post: 12-01-2007, 02:06 AM
  5. Is valgrind always right?
    By g4j31a5 in forum Linux Programming
    Replies: 3
    Last Post: 07-16-2007, 10:39 PM