I'm experiencing a strange problem in one of my projects. My code base depends on an external library, which contains a class named Dataset
. The Dataset
class privately inherits from std::vector<Sample>
(where Sample
is another custom class defined in the library).
Moreover, such a class exposes a Save
member function in order to serialize the data composing the data set into a text file. The Save
member function is defined as follows:
inline void Dataset::Save(string filename, ModalityType modality)
{
ofstream log_file;
if (modality == OVERWRITE) {
log_file.open(filename.c_str());
} else {
log_file.open(filename.c_str(), ios::out | ios::app);
}
if (log_file.is_open()) {
log_file << *(this);
}
log_file.close();
}
ofstream& operator<< (ofstream& out, Dataset& ds)
{
unsigned int size = ds.size();
unsigned int input_size = ds.GetInputSize();
unsigned int output_size = ds.GetOutputSize();
out << input_size << " " << output_size << endl;
for (unsigned int i = 0; i < size; i++) {
Sample* s = ds[i];
for (unsigned int j = 0; j < input_size; j++) {
out << s->GetInput(j) << " ";
}
for (unsigned int j = 0; j < output_size; j++) {
out << s->GetOutput(j) << " ";
}
out << endl;
}
return out;
}
Both my code and the external library have been compiled under OS X 10.8.2 with clang 4.2 and flags -std=c++11 -stdlib=libc++
. I need to do so, since my code base uses several C++11 facilities (e.g., random
). Moreover, the library itself depends on boost, which in turn has been compiled with clang and C++11 support.
Everything compiles and works as expected by using the following Makefile:
CXX = clang++
CXXDIALECT = -std=c++11 -stdlib=libc++
DEFS = -DBOOST_NO_CXX11_NUMERIC_LIMITS
INCLUDE_DIRS = -I. -I/usr/local/include -I/usr/include -I/opt/local/include
LIB_DIRS = -L/usr/local/lib -L/opt/local/lib
LIBS = -lfitted -lgsl -lgslcblas -lboost_thread -lboost_program_options -lboost_regex -lboost_system
CINCLUDE = $(INCLUDE_DIRS)
CXXFLAGS = -Os $(CXXDIALECT) $(CINCLUDE) $(DEFS)
tests := main.cpp
sources := $(filter-out $(tests), $(wildcard *.cpp))
objects := $(patsubst %.cpp,%.o,$(sources))
main: $(objects)
$(CXX) $(CXXFLAGS) -o $@ [email protected] $(objects) $(LIBS) $(LIB_DIRS) -v
%.o : %.cpp
$(CXX) -c $(CPPFLAGS) $(CXXFLAGS) $< -o $@
where libfitted
is the name of the external library.
Nevertheless, I'm developing my code under XCode 4.6.2. The problem is that everytime I try run/debug the code in XCode, the Dataset.save
member function triggers the following error:
and an empty dataset.txt
file is created on disk. Here's a couple of screenshots of the stack trace:
Click here and here to view them in full size.
As reported in the screenshots, the problem seems to be in the ofstream.flush()
member function.
Finally, I report the output of both make and xcodebuild
I really can't figure out why the same code, with the same compiler and libraries, executes correctly if compiled with the afore-mentioned Makefile, while it is not working if executed in XCode.
UPDATE #1: I just noticed that the code is debuggable in XCode (although not runnable) if I use as debugger GDB instead of LLDB, i.e., Dataset.save
does not trigger the EXC_BAD_ACCESS error.
UPDATE #2: I recompiled the library with the -g -O0
flags so as to preserve debug symbols. The problem is that everytime a ofstream
object is initialized in a Dataset
's member function, the this
pointer of the Dataset
instance becomes NULL
, i.e., the Dataset
object is nullified. Consequently, each attempt to access any data member within the member function results in a EXC_BAD_ACCESS
. This is one of the weirdest thing I've ever seen. Any idea on why this is happening?
Thanks.
-std=c++11 -stdlib=libc++
) as XCode. Moreover, the source files are the same (I simply run the Makefile in the folder where the source files are stored). Regarding the warnings, in XCode many warning flags are active by default, and indeed I obtain several warnings. Nevertheless, all warning are about the boost library, rather than the classes composing thelibfitted
library per se. For the sake of completeness, I will add the output of bothmake
andxcodebuild
– burton0 Jun 2 '13 at 11:23ofstream.flush()
member function – burton0 Jun 2 '13 at 13:40