 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Your Name Here Guest
|
Posted: Wed Jan 21, 2004 9:17 am Post subject: Problem with -fPIC in gcc |
|
|
Hello all!
It appears to me that the -fPIC option induces a memory leak
in egcs-2.91.66 (apologies for the old version). I was curious if
someone might look this over and make sure I'm not just ignorant.
I'd like to know if this happens on gcc 3.0 or later before I start
complaining on bugzilla or whatever. Very short example follows with
a makefile creating two programs, the first, "leak" should run smoothly
(the second column is relatively constant), while the second "leak"
clearly demonstrates a successively decreasing amount of memory (there's
probably a better way to demonstrate this, but this works for now).
Note the distinction between the class leakr being placed in the same
directory vs. a separate directory.
If you take out "-fpic", then both programs work.
Of course, the original intent was to make a shared library and
then the makefile would be different (I'd be linking with a .a instead
of a .o), but that shouldn't matter, should it?
Thanks for your help,
Andrew
::::::::::::::
leak.cpp
::::::::::::::
#include <iostream>
#include <cmath>
#include "leakr.h"
using namespace std;
int main(void) {
leakr ap;
for(int i=0;i<10000;i++) {
ap.calc_ez("");
if (i%1000==0) {
cout << i << "t" << flush;
system("free | grep "Mem" | awk '{print $4}'");
}
}
return 0;
}
::::::::::::::
leak2.cpp
::::::::::::::
#include
#include <cmath>
#include "lib/leakr.h"
using namespace std;
int main(void) {
leakr ap;
for(int i=0;i<10000;i++) {
ap.calc_ez("");
if (i%1000==0) {
cout << i << "t" << flush;
system("free | grep "Mem" | awk '{print $4}'");
}
}
return 0;
}
::::::::::::::
leakr.cpp
::::::::::::::
#include "leakr.h"
using namespace std;
int leakr::calc_ez(std::string stype) {
for(int kk=0;kk<1000;kk++) {
double d=sin(cos(0.5));
}
return 0;
}
::::::::::::::
leakr.h
::::::::::::::
#include
#include <string>
class leakr {
public:
leakr() {};
virtual int calc_ez(std::string stype);
};
::::::::::::::
lib/leakr.cpp
::::::::::::::
#include "leakr.h"
using namespace std;
int leakr::calc_ez(std::string stype) {
for(int kk=0;kk<1000;kk++) {
double d=sin(cos(0.5));
}
return 0;
}
::::::::::::::
lib/leakr.h
::::::::::::::
#include
#include <string>
class leakr {
public:
leakr() {};
virtual int calc_ez(std::string stype);
};
::::::::::::::
makefile
::::::::::::::
all: leak leak2
leakr.o:
g++ -fpic -c leakr.cpp
leak.o:
g++ -c leak.cpp
leak: leak.o leakr.o
g++ -o leak leak.cpp leakr.cpp
lib/leakr.o:
cd lib; g++ -fpic -c leakr.cpp
leak2.o:
g++ -c leak2.cpp
leak2: leak2.o lib/leakr.o
g++ -o leak2 leak2.o lib/leakr.o
clean:
-rm *.o lib/*.o leak leak2 *~ lib/*~
leak.tar.gz:
tar cvzf leak.tar.gz lib/*.cpp lib/*.h *.cpp *.h makefile
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Raoul Gough Guest
|
Posted: Fri Jan 23, 2004 3:00 pm Post subject: Re: Problem with -fPIC in gcc |
|
|
[email]eknecronzontas (AT) excite (DOT) com[/email] (Your Name Here) writes:
| Quote: | Hello all!
It appears to me that the -fPIC option induces a memory leak
in egcs-2.91.66 (apologies for the old version). I was curious if
someone might look this over and make sure I'm not just ignorant.
I'd like to know if this happens on gcc 3.0 or later before I start
complaining on bugzilla or whatever.
|
Yes, I doubt anyone would try to fix a bug in that old a version of
gcc now. Upgrading the compiler would have to be the easiest option,
unless there is a very good reason not to.
| Quote: | Very short example follows with
a makefile creating two programs, the first, "leak" should run smoothly
(the second column is relatively constant), while the second "leak"
clearly demonstrates a successively decreasing amount of memory (there's
probably a better way to demonstrate this, but this works for now).
Note the distinction between the class leakr being placed in the same
directory vs. a separate directory.
If you take out "-fpic", then both programs work.
Of course, the original intent was to make a shared library and
then the makefile would be different (I'd be linking with a .a instead
of a .o), but that shouldn't matter, should it?
Thanks for your help,
Andrew
::::::::::::::
leak.cpp
::::::::::::::
#include
#include
#include "leakr.h"
using namespace std;
int main(void) {
leakr ap;
for(int i=0;i<10000;i++) {
ap.calc_ez("");
|
[snip]
| Quote: | int leakr::calc_ez(std::string stype) {
for(int kk=0;kk<1000;kk++) {
double d=sin(cos(0.5));
}
return 0;
}
[snip] |
I didn't see anything obviously wrong with the code (but I haven't
tested it). If you want to investigate the problem further, I would
suggest getting more detailed information about where memory
allocation is happening, and why the deallocation isn't working - you
should be able to start by replacing global new, delete, new[] and
delete[]. Alternatively, your C library might support mallinfo() which
would probably give you a good idea of where allocations are happening
(assuming that the default new and new[] use malloc). You could then
set a break point where the corresponding deallocation should be
happening, and see whether it ever gets there or not. Then again, why
not just upgrade to a compiler that fixes the problem?
--
Raoul Gough.
export LESS='-X'
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|