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