gpt4 book ai didi

c++ - 如果使用 Makefile 编译,代码可以正常工作,如果使用 XCode 编译,代码会崩溃

转载 作者:太空狗 更新时间:2023-10-29 23:06:08 25 4
gpt4 key购买 nike

我在我的一个项目中遇到了一个奇怪的问题。我的代码库依赖于一个外部库,其中包含一个名为 Dataset 的类. Dataset类私有(private)继承自 std::vector<Sample> (其中 Sample 是库中定义的另一个自定义类)。

此外,这样的类公开了一个 Save成员函数,以便将构成数据集的数据序列化为文本文件。 Save成员函数定义如下:

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;
}

我的代码和外部库都是在 OS X 10.8.2 下编译的,带有 clang 4.2 和标志 -std=c++11 -stdlib=libc++ .我需要这样做,因为我的代码库使用了多个 C++11 工具(例如 random )。此外,该库本身依赖于 boost,而 boost 又使用 clang 和 C++11 支持进行编译。

使用以下 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 $@ $@.cpp $(objects) $(LIBS) $(LIB_DIRS) -v

%.o : %.cpp
$(CXX) -c $(CPPFLAGS) $(CXXFLAGS) $< -o $@

哪里libfitted是外部库的名称。

尽管如此,我还是在 XCode 4.6.2 下开发我的代码。问题是每次我尝试运行/调试 XCode 中的代码时,Dataset.save成员函数触发以下错误:

EXC_BAD_ACCESS

和一个空的 dataset.txt文件在磁盘上创建。这是堆栈跟踪的几个屏幕截图:

stack trace1 stack trace2

点击herehere以全尺寸查看它们。

正如屏幕截图中所报告的,问题似乎ofstream.flush()中。成员函数。最后,我报告了 make 的输出和 xcodebuild

我真的不明白为什么同样的代码,同样的编译器和库,如果用前面提到的 Makefile 编译,可以正确执行,而如果在 XCode 中执行,却不能正常工作。

更新 #1: 我刚刚注意到,如果我使用 GDB 而不是 LLDB 作为调试器,代码在 XCode 中是可调试的(尽管不可运行),即 Dataset.save不会触发 EXC_BAD_ACCESS 错误。

更新 #2: 我用 -g -O0 重新编译了库标志以便保留调试符号。问题是每次 ofstream对象在 Dataset 中初始化的成员函数,this Dataset 的指针实例变为 NULL ,即 Dataset对象被取消。因此,每次尝试访问成员函数内的任何数据成员都会导致 EXC_BAD_ACCESS。 .这是我见过的最奇怪的事情之一。知道为什么会这样吗?

谢谢。

最佳答案

std::vector<...> 类并非旨在用作基类。如此使用它会导致未定义的行为(至少是因为缺少虚拟析构函数)。

C++ 编码标准(Sutter、Alexandrescu)中的第 35 项,已命名

Avoid inheriting from classes that were not designed to be base classes.

在这里可能会有帮助。

Effective C++ (Meyers) 的第 7 条讨论了这个问题:

class SpecialString: public std::string{
...
}

At first glance, this may look innocuous, but if anywhere in an application you somehow convert a pointer-to-SpecialString into a pointer-to-string and you then use delete on the string pointer, you are instantly transported to the realm of undefined behavior.

关于c++ - 如果使用 Makefile 编译,代码可以正常工作,如果使用 XCode 编译,代码会崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16882160/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com