LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Problem with dmalloc Library (https://www.linuxquestions.org/questions/programming-9/problem-with-dmalloc-library-820112/)

amir1981 07-15-2010 11:45 PM

Problem with dmalloc Library
 
Hello experts,
I'm trying to use dmalloc (http://dmalloc.com) library to debug a program that I suspect is suffering from some kind of memory leak. To do this, I have wrote a very simple program to see how dmalloc is working!
I have runed this program on 2 platforms 1)Ubuntu 32 bit 2)Suse 64 bit
and it gives completely different results on these two platforms! Ultimately I need to run it on Suse, but I wonder why results are not the same on these platforms , and moreover they are not expected results.
1-Ubuntu:
test.c
Code:

#include <stdio.h>
#include <stdlib.h>
void myfunc();
#ifdef DMALLOC
#include "dmalloc.h"
#endif
int main()
{
int *test,i,j;
test=(int*)malloc(sizeof(int)*2);
for(i=0;i<10;i++) test[i]=i;
for (j=9;j>=0;j--) printf(" %d ",test[j]);
free(test);
myfunc();
return 1;
}

myfunc.c
Code:

#include <stdio.h>
#include <stdlib.h>
void myfunc();
#ifdef DMALLOC
#include "dmalloc.h"
#endif
void myfunc()
{
int *test,i,j;
printf("\n");
test=(int*)malloc(sizeof(int)*2);
for(i=0;i<10;i++)test[i]=i;
for (j=9;j>=0;j--)printf(" %d ",test[j]);
free(test);
printf("\n");
}

makefile:
Code:

CC=g++
CFLAGS=-c -Wall -fpermissive -DDMALLOC -DDMALLOC_FUNC_CHECK
LDFLAGS=
SOURCES= myfunc.c test.c libdmallocxx.a
OBJECTS=$(SOURCES:.c=.o)
EXECUTABLE=test
all: $(SOURCES) $(EXECUTABLE)       
$(EXECUTABLE): $(OBJECTS) 
        $(CC) $(LDFLAGS) $(OBJECTS) -o $@
.c.o:
        $(CC) $(CFLAGS) $< -o $@

at the beginning I run dmalloc -l logfile ( I already run "function dmalloc { eval `command dmalloc -b $*`; }" as stated in docs)

here is the results after running:
Quote:

amir-laptop_[1]: ./test
Segmentation fault
amir-laptop_[1]: ./test
Segmentation fault
amir-laptop_[1]: ./test
Segmentation fault
amir-laptop_[1]: ./test
Segmentation fault
amir-laptop_[1]: ./test
Segmentation fault
amir-laptop_[1]: ./test
Segmentation fault
amir-laptop_[1]: ./test
Segmentation fault
amir-laptop_[1]: ./test
Segmentation fault
amir-laptop_[1]: ./test
Segmentation fault
amir-laptop_[1]: ./test
Segmentation fault
amir-laptop_[1]: ./test
debug-malloc library: dumping program, fatal error
Error: failed OVER picket-fence magic-number check (err 27)
9 8 7 6 5 4 3 2 1 0 Aborted
As you can see I have runned test 10 times and in these runs it gives a segmentation fault error and does NOT generate a logfile
after that in 11th run it generate the logfile and gives another kind of error, WHAT 's the problem? Why this happen? I expect that it generate a logfile everytime not just by chance


On SUSE-64 bit:
Here the problem is even worse. When I do not use dmalloc the program generate kind of error:
output without dmalloc:
Quote:

euler_[1]: make
g++ -c -Wall -fpermissive myfunc.c -o myfunc.o
g++ -c -Wall -fpermissive test.c -o test.o
test.c: In function ‘int main()’:
test.c:16: warning: ‘test’ may be used uninitialized in this function
g++ myfunc.o test.o -o test
euler_[1]: ./test
*** glibc detected *** free(): invalid pointer: 0x0000002a9566b1c0 ***
9 8 7 6 5 4 3 2 1 0
*** glibc detected *** free(): invalid next size (fast): 0x0000000000501010 ***
9 8 7 6 5 4 3 2 1 0
This is the output with dmalloc:

Quote:

euler_[1]: ./test
9 8 7 6 5 4 3 2 1 0
9 8 7 6 5 4 3 2 1 0
This time we have a logfile but it doen not show any error!!!
Quote:

euler_[1]: cat logfile
1279255144: 4: Dmalloc version '5.5.2' from 'http://dmalloc.com/'
1279255144: 4: flags = 0, logfile 'logfile'
1279255144: 4: interval = 0, addr = 0, seen # = 0, limit = 0
1279255144: 4: starting time = 1279255144
1279255144: 4: process pid = 784
1279255144: 4: ending time = 1279255144, elapsed since start = 0:00:00
What's wrong here?

Thanks,
amir

paulsm4 07-16-2010 12:39 AM

Dude - just because a program has a bug, doesn't necessarily mean it's going to CRASH.

It'll crash if you're LUCKY (that way, at least you KNOW something is wrong!).

So if you:
1) allocate 2 integers
... and then ...
2) use 10 integers

That's pretty much the same as:
1) putting on a blindfold
... and then ...
2) flooring the accelerator

Sometimes you might hit a hedge. Other times you might slam into a brick wall. It depends on the compiler, it depends on the platform ;)

Just don't expect deterministic results for an irrational scenario, OK? ;)

Wim Sturkenboom 07-16-2010 01:04 AM

I think the question is more why dmalloc does not behave as expected. I suggest that OP visits their forum (if it works); I tried and get database errors, tried to report is and got email errors (relaying denied). Maybe the author must be contcted via his own webpage (link on the dmalloc page).


All times are GMT -5. The time now is 11:14 PM.