Hi all,
I write because I am struggling with a very strange thing. I have this class:
static const long POINTS_VERY_FEW = 100;
class Bin2DSphere {
/*! Class for 2-dimensional binning on a sphere */
public:
string Name;
double PhiMin; /* Minimum value of Phi */
double PhiMax; /* Maximum value of Phi */
double ThetaMin; /* Minimum value of Theta */
double ThetaMax; /* Maximum value of Theta */
long Size; /* Size of data */
bool IsFilled; /* Flag is TRUE if histogram has been filled */
bool IsNormalized; /* Flag is TRUE if histogram has been normalized */
bool IsKernel; /* Flag is TRUE if kernel technique is used */
double NormalizationFactor; /* Normalization factor */
long *PhiBins; /* Number of bins in Phi */
long *OPhiBins; /* Number of bins in Phi for kernel density */
long ThetaBins; /* Number of bins in Theta */
long OThetaBins; /* Number of bins in Theta for kernel density */
long Bins; /* Number of total bins */
double Omega; /* Solid angle */
double Theta; /* Angular scale covered by Omega */
double **ParameterPhi; /* Parameter Phi for binning */
double *ParameterTheta; /* Parameter Theta for binning */
double **OParameterPhi; /* Parameter Phi for overbinning */
double *OParameterTheta; /* Parameter Theta for overbinning */
double **PDF; /* Probability distribution function */
//double CDF []; /* Cumulative distribution function */
double **KernelGaussian; /* Gaussian kernel density estimate */
int O; /* Number of bins for resampling needed in Gaussian Kernel */
double S; /* Bandwith of Gaussian Kernel */
Bin2DSphere () {
/*! Default constructor */
PhiBins = new long [POINTS_VERY_FEW];
OPhiBins = new long [POINTS_VERY_FEW];
ParameterPhi = new double*[POINTS_VERY_FEW];
for (int i = 0; i < POINTS_VERY_FEW; ++i ) ParameterPhi[i] = new double [POINTS_VERY_FEW];
OParameterTheta = new double [POINTS_VERY_FEW];
OParameterPhi = new double*[POINTS_VERY_FEW];
for (int i = 0; i < POINTS_VERY_FEW; ++i ) OParameterPhi[i] = new double [POINTS_VERY_FEW];
ParameterTheta = new double [POINTS_VERY_FEW];
PDF = new double*[POINTS_VERY_FEW];
for (int i = 0; i < POINTS_VERY_FEW; ++i ) PDF[i] = new double [POINTS_VERY_FEW];
//CDF = new double*[POINTS_FEW];
//for (int i = 0; i < POINTS_VERY_FEW; ++i ) CDF[i] = new double[POINTS_FEW];
KernelGaussian = new double*[POINTS_VERY_FEW];
for (int i = 0; i < POINTS_VERY_FEW; ++i ) KernelGaussian[i] = new double [POINTS_VERY_FEW];
Name = "";
O = 2;
PhiMin = ThetaMin = 1e+100;
PhiMax = ThetaMax = -1e+100;
NormalizationFactor = -1e+100;
IsFilled = FALSE;
IsNormalized = FALSE;
IsKernel = FALSE;
};
~Bin2DSphere() {
/*! Default destructor */
//cerr << Name << ": BinSphere Destructor" << endl;
}
void Bin2DSphereFree() {
/*!
Method to free allocated objects: code is not in the destructor
to avoid using deallocated memory
*/
cerr << "bin: 1" << endl;
delete [] KernelGaussian[0];
delete [] KernelGaussian;
cerr << "bin: 2" << endl;
delete [] PDF[0];
delete [] PDF;
cerr << "bin: 3" << endl;
delete [] OParameterTheta;
cerr << "bin: 4" << endl;
delete [] OParameterPhi[0];
delete [] OParameterPhi;
cerr << "bin: 5" << endl;
delete [] ParameterTheta;
cerr << "bin: 6" << endl;
delete [] ParameterPhi[0];
delete [] ParameterPhi;
cerr << "bin: 7" << endl;
delete [] OPhiBins;
cerr << "bin: 8" << endl;
delete [] PhiBins;
//cerr << Name << ": BinSphere Free" << endl;
//delete [] CDF;
};
}
and, in some piece of my main code, I have:
.....
Bin2DSphere TargetDensity;
Bin2DSphere (*ExpectedDensity) = new Bin2DSphere [POINTS_VERY_FEW];
Bin2DSphere IsotropicDensity;
....
/* Free objects */
cerr << "3" << endl;
IsotropicDensity.Bin2DSphereFree();
cerr << "4" << endl;
delete [] RMSIsotrop;
delete [] MeanIsotrop;
//delete [] IsoValue;
for(l=MinBin; l<=MaxBin; l+=StepBin){
ExpectedDensity[l].Name = "pippo";
ExpectedDensity[l].Bin2DSphereFree();
}
cerr << "5" << endl;
delete [] ExpectedDensity;
cerr << "6" << endl;
TargetDensity.Bin2DSphereFree();
cerr << "7" << endl;
My code compiles and runs perfectly on Mac OS X:
Darwin NAME 9.8.0 Darwin Kernel Version 9.8.0: Wed Jul 15 16:55:01 PDT 2009; root:xnu-1228.15.4~1/RELEASE_I386 i386
g++ -v
Using built-in specs.
Target: i686-apple-darwin9
Configured with: /var/tmp/gcc/gcc-5465~16/src/configure --disable-checking -enable-werror --prefix=/usr --mandir=/share/man --enable-languages=c,objc,c++,obj-c++ --program-transform-name=/^[cg][^.-]*$/s/$/-4.0/ --with-gxx-include-dir=/include/c++/4.0.0 --with-slibdir=/usr/lib --build=i686-apple-darwin9 --with-arch=apple --with-tune=generic --host=i686-apple-darwin9 --target=i686-apple-darwin9
Thread model: posix
gcc version 4.0.1 (Apple Inc. build 5465)
on Ubuntu (proc. AMD64):
Linux NAME 2.6.27-17-generic #1 SMP Fri Mar 12 02:08:25 UTC 2010 x86_64 GNU/Linux
g++ -v
Using built-in specs.
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --enable-languages=c,c++,fortran,objc,obj-c++,treelang --prefix=/usr --enable-shared --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --enable-nls --with-gxx-include-dir=/usr/include/c++/4.2 --program-suffix=-4.2 --enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc --enable-mpfr --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.2.4 (Ubuntu 4.2.4-1ubuntu4)
and on
Linux NAME 2.6.24-28-generic #1 SMP Wed Nov 24 09:00:20 UTC 2010 x86_64 GNU/Linux
g++ -v
Using built-in specs.
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --enable-languages=c,c++,fortran,objc,obj-c++,treelang --prefix=/usr --enable-shared --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --enable-nls --with-gxx-include-dir=/usr/include/c++/4.2 --program-suffix=-4.2 --enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc --enable-mpfr --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.2.4 (Ubuntu 4.2.4-1ubuntu4)
But... it does not work on:
Linux NAME 2.6.24-28-generic #1 SMP Wed Nov 24 09:30:14 UTC 2010 i686 GNU/Linux
g++ -v
Using built-in specs.
Target: i486-linux-gnu
Configured with: ../src/configure -v --enable-languages=c,c++,fortran,objc,obj-c++,treelang --prefix=/usr --enable-shared --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --enable-nls --with-gxx-include-dir=/usr/include/c++/4.2 --program-suffix=-4.2 --enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc --enable-mpfr --enable-targets=all --enable-checking=release --build=i486-linux-gnu --host=i486-linux-gnu --target=i486-linux-gnu
Thread model: posix
gcc version 4.2.4 (Ubuntu 4.2.4-1ubuntu4)
The output of the few lines till the segmentation fault (the first part of the program works perfectly) is:
3
bin: 1
bin: 2
bin: 3
bin: 4
bin: 5
bin: 6
bin: 7
bin: 8
4
bin: 1
bin: 2
bin: 3
bin: 4
bin: 5
bin: 6
bin: 7
bin: 8
bin: 1
bin: 2
bin: 3
bin: 4
bin: 5
bin: 6
bin: 7
bin: 8
bin: 1
bin: 2
bin: 3
bin: 4
bin: 5
bin: 6
bin: 7
bin: 8
bin: 1
bin: 2
bin: 3
bin: 4
bin: 5
bin: 6
bin: 7
bin: 8
bin: 1
bin: 2
bin: 3
bin: 4
bin: 5
bin: 6
bin: 7
bin: 8
bin: 1
bin: 2
bin: 3
bin: 4
bin: 5
bin: 6
bin: 7
bin: 8
bin: 1
bin: 2
bin: 3
bin: 4
bin: 5
bin: 6
bin: 7
bin: 8
bin: 1
bin: 2
bin: 3
bin: 4
bin: 5
bin: 6
bin: 7
bin: 8
bin: 1
bin: 2
bin: 3
bin: 4
bin: 5
bin: 6
bin: 7
bin: 8
bin: 1
bin: 2
bin: 3
bin: 4
bin: 5
bin: 6
bin: 7
bin: 8
bin: 1
bin: 2
bin: 3
bin: 4
bin: 5
bin: 6
bin: 7
bin: 8
bin: 1
bin: 2
bin: 3
bin: 4
bin: 5
bin: 6
bin: 7
bin: 8
bin: 1
bin: 2
bin: 3
bin: 4
bin: 5
bin: 6
bin: 7
bin: 8
bin: 1
bin: 2
bin: 3
bin: 4
bin: 5
bin: 6
bin: 7
bin: 8
bin: 1
bin: 2
bin: 3
bin: 4
bin: 5
bin: 6
bin: 7
bin: 8
bin: 1
bin: 2
bin: 3
bin: 4
bin: 5
bin: 6
bin: 7
bin: 8
bin: 1
bin: 2
bin: 3
bin: 4
bin: 5
bin: 6
bin: 7
bin: 8
bin: 1
bin: 2
bin: 3
bin: 4
bin: 5
bin: 6
bin: 7
bin: 8
bin: 1
bin: 2
bin: 3
bin: 4
bin: 5
bin: 6
bin: 7
bin: 8
bin: 1
bin: 2
bin: 3
bin: 4
bin: 5
bin: 6
bin: 7
bin: 8
bin: 1
bin: 2
bin: 3
bin: 4
bin: 5
bin: 6
bin: 7
bin: 8
bin: 1
bin: 2
bin: 3
bin: 4
bin: 5
bin: 6
bin: 7
bin: 8
bin: 1
bin: 2
bin: 3
bin: 4
bin: 5
bin: 6
bin: 7
bin: 8
bin: 1
bin: 2
bin: 3
bin: 4
bin: 5
bin: 6
bin: 7
bin: 8
bin: 1
bin: 2
bin: 3
bin: 4
bin: 5
bin: 6
bin: 7
bin: 8
bin: 1
bin: 2
bin: 3
bin: 4
bin: 5
bin: 6
bin: 7
bin: 8
bin: 1
bin: 2
bin: 3
bin: 4
bin: 5
bin: 6
bin: 7
bin: 8
bin: 1
bin: 2
bin: 3
bin: 4
bin: 5
bin: 6
bin: 7
bin: 8
bin: 1
bin: 2
bin: 3
bin: 4
bin: 5
bin: 6
bin: 7
bin: 8
bin: 1
bin: 2
bin: 3
bin: 4
bin: 5
bin: 6
bin: 7
bin: 8
bin: 1
bin: 2
bin: 3
bin: 4
bin: 5
bin: 6
bin: 7
bin: 8
bin: 1
bin: 2
bin: 3
bin: 4
bin: 5
bin: 6
bin: 7
bin: 8
bin: 1
bin: 2
bin: 3
bin: 4
bin: 5
bin: 6
bin: 7
bin: 8
bin: 1
bin: 2
bin: 3
bin: 4
bin: 5
bin: 6
bin: 7
bin: 8
bin: 1
bin: 2
bin: 3
bin: 4
bin: 5
bin: 6
bin: 7
bin: 8
bin: 1
bin: 2
bin: 3
bin: 4
bin: 5
bin: 6
bin: 7
bin: 8
bin: 1
bin: 2
bin: 3
bin: 4
bin: 5
bin: 6
bin: 7
bin: 8
bin: 1
bin: 2
bin: 3
bin: 4
bin: 5
bin: 6
bin: 7
bin: 8
bin: 1
bin: 2
bin: 3
bin: 4
bin: 5
bin: 6
bin: 7
bin: 8
bin: 1
bin: 2
bin: 3
bin: 4
bin: 5
bin: 6
bin: 7
bin: 8
bin: 1
bin: 2
bin: 3
bin: 4
bin: 5
bin: 6
bin: 7
bin: 8
bin: 1
bin: 2
bin: 3
bin: 4
bin: 5
bin: 6
bin: 7
bin: 8
5
6
bin: 1
bin: 2
bin: 3
bin: 4
bin: 5
bin: 6
bin: 7
bin: 8
*** glibc detected *** SDAFp: double free or corruption (out): 0x0806b6e0 ***
======= Backtrace: =========
/lib/tls/i686/cmov/libc.so.6[0xb752ab25]
/lib/tls/i686/cmov/libc.so.6(cfree+0x90)[0xb752e590]
/usr/lib/libstdc++.so.6(_ZdlPv+0x21)[0xb76f6b11]
/usr/lib/libstdc++.so.6(_ZdaPv+0x1d)[0xb76f6b6d]
SDAFp[0x8058980]
SDAFp[0x804f253]
SDAFp[0x804fc90]
/lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xe0)[0xb74d5450]
SDAFp(__gxx_personality_v0+0x9d)[0x8049f11]
======= Memory map: ========
08048000-08063000 r-xp 00000000 00:19 2555967973 /PATH/SDAFp
08063000-08064000 rw-p 0001b000 00:19 2555967973 /PATH/SDAFp
08064000-0a05c000 rw-p 08064000 00:00 0 [heap]
7e000000-7e021000 rw-p 7e000000 00:00 0
7e021000-7e100000 ---p 7e021000 00:00 0
7e134000-b74bf000 rw-p 7e134000 00:00 0
b74bf000-b7608000 r-xp 00000000 08:02 1849396 /lib/tls/i686/cmov/libc-2.7.so
b7608000-b7609000 r--p 00149000 08:02 1849396 /lib/tls/i686/cmov/libc-2.7.so
b7609000-b760b000 rw-p 0014a000 08:02 1849396 /lib/tls/i686/cmov/libc-2.7.so
b760b000-b760e000 rw-p b760b000 00:00 0
b760e000-b7618000 r-xp 00000000 08:02 1312900 /lib/libgcc_s.so.1
b7618000-b7619000 rw-p 0000a000 08:02 1312900 /lib/libgcc_s.so.1
b7619000-b761a000 rw-p b7619000 00:00 0
b761a000-b763d000 r-xp 00000000 08:02 1849401 /lib/tls/i686/cmov/libm-2.7.so
b763d000-b763f000 rw-p 00023000 08:02 1849401 /lib/tls/i686/cmov/libm-2.7.so
b763f000-b7727000 r-xp 00000000 08:02 1284226 /usr/lib/libstdc++.so.6.0.9
b7727000-b772a000 r--p 000e8000 08:02 1284226 /usr/lib/libstdc++.so.6.0.9
b772a000-b772c000 rw-p 000eb000 08:02 1284226 /usr/lib/libstdc++.so.6.0.9
b772c000-b7732000 rw-p b772c000 00:00 0
b7747000-b7749000 rw-p b7747000 00:00 0
b7749000-b774a000 r-xp b7749000 00:00 0 [vdso]
b774a000-b7764000 r-xp 00000000 08:02 1313017 /lib/ld-2.7.so
b7764000-b7766000 rw-p 00019000 08:02 1313017 /lib/ld-2.7.so
bf959000-bf96e000 rw-p bffea000 00:00 0 [stack]
Aborted
Time for questions..:
1) Why does it work on some hardware/software configurations only?
2) Why the problem does not affect the program every time I call the Bin2DSphereFree() but just the last call, and in particular in the last delete [] of the method?
Thank you in advance for your help!