LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-15-2010, 11:45 PM   #1
amir1981
LQ Newbie
 
Registered: Jul 2010
Posts: 19

Rep: Reputation: 0
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

Last edited by amir1981; 07-15-2010 at 11:47 PM.
 
Old 07-16-2010, 12:39 AM   #2
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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?
 
Old 07-16-2010, 01:04 AM   #3
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
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).
 
  


Reply

Tags
debug, linux, malloc, memory, suse, ubuntu


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to test kernel memory leak using dmalloc laginagesh Linux - Kernel 2 06-11-2010 02:28 PM
install from source - unable to find the libcurl library - but library is installed pulper Linux - Newbie 2 02-23-2009 09:00 PM
how to use dmalloc for debugging memory leak on Linux platform? aaratik Red Hat 3 05-22-2007 05:47 PM
LINUX - linking archive (static library) with shared (dynamic) library gurkama Programming 5 03-04-2007 11:11 PM
howto compile bin with my library using all-static and shared linked standart library stpg Programming 4 06-29-2004 04:20 AM


All times are GMT -5. The time now is 02:36 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration