永远悬挂的灾难
Forever hanging destructor
本文关键字:永远 更新时间:2023-10-16
不过,评论一些从未执行的函数定义的行都可以修复它。怎么来?
完整的最小示例我在下面删除无关代码时结束了。
int main() {
auto tList = tokenize();
tList.front()->~Token(); //hangs forever if the code 10 lines above is not commented, normally I'll have pop_front() here
cout << "never gets printed";
return 0;
}
通常我会拥有
tList.pop_front()
而不是
tList.front()->~Token();
这里只是为了更明确地演示
承诺的代码:(也https://gist.github.com/anonymon/3b8fa11e90c617b35623ba54320505432050c3e#file-main-cpp-l68)tokenizer.h
#include <list>
#include <memory>
using namespace std;
#ifndef SGREP_TOKENIZER_H
#define SGREP_TOKENIZER_H
class Token {
public:
virtual ~Token() = default;
virtual bool isChar() const {
return false;
}
};
using TTokenList = list<unique_ptr<Token>>;
TTokenList tokenize ();
#endif //SGREP_TOKENIZER_H
tokenizer.cpp
#include <iostream>
#include <cctype>
#include <string>
#include "tokenizer.h"
using namespace std;
class Char : public Token {
char value;
public:
Char (char value) : value(value){};
bool isChar() const override {
return true;
}
};
TTokenList tokenize () {
TTokenList tList;
tList.push_back(make_unique<Char>('h'));
return tList;
}
main.cpp
#include <iostream>
#include <string>
#include <vector>
#include <memory>
#include <exception>
#include "tokenizer.h"
using namespace std;
class SyntaxError : public exception {};
class Regex {
public:
virtual size_t match_part(const string& str, size_t begin, size_t maxLen, vector<size_t>& groups) = 0; // groups is a vector of begin,end indices of matched groups (ParenRegexes) (to be able to recreate substrings)
virtual ~Regex() = default;
};
class Char : public Regex {
protected:
vector<char> characterRanges; // pairs -> lower and upper inclusive bound of matching a character (even lower, odd upper), size is always divisible by 2
public:
Char(TTokenList& t, size_t& groupCount) {}
size_t match_part(const string& str, size_t begin, size_t maxLen, vector<size_t>& groups) override {
return 1;
}
};
class SeqRegex: public Regex {
unique_ptr<Regex> a;
public:
SeqRegex(TTokenList& t, size_t& groupCount);
size_t match_part(const string& str, size_t begin, size_t maxLen, vector<size_t>& groups) override { // maxLen by nemelo byt 0:
return 1;
}
};
class OrRegex: public Regex {
SeqRegex a;
public:
OrRegex(TTokenList& t, size_t& groupCount) : a(t, groupCount) {}
size_t match_part(const string& str, size_t begin, size_t maxLen, vector<size_t>& groups) override {
return 1;
}
};
SeqRegex::SeqRegex(TTokenList& t, size_t& groupCount) {
if (t.front()->isChar()) { // Commenting out this portion of code fixes the forever hanging ..
a = make_unique<Char>(t, groupCount);
} else {
cerr << "syntax error, expected [^)]-*^|]" << endl;
throw SyntaxError();
} // ...commenting out up to this point
}
int main() {
auto tList = tokenize();
tList.front()->~Token(); //hangs forever if the code 10 lines above is not commented, normally I'll have pop_front() here
cout << "never gets printed";
return 0;
}
总体上,也许有十几个唯一的方案,可以使用~T()
语法直接调用对象的破坏者。
这不是这样的情况。
如果您的目的是删除列表中的第一个令牌,则只需调用tList.pop_front()
就足够了。
当std::unique_ptr
对象稍后将其再次删除它时,直接调用驱动器即将调用不确定的行为。因此,当该代码被编译/执行时,您无法保证会发生什么或不会发生的事情。
您有两个称为Char
的类。他们没有相同的定义。这违反了一个定义规则(ODR),因此违反了行为。
我只是好奇为什么当您将文件合并在一起时您没有立即发现这一点。
相关文章:
- 为什么是谷神星协方差.计算()似乎永远运行而不返回?
- 密码登录程序将永远循环并显示不正确的结果
- 我想在C++中读取一些多个字符,但它永远不会读取第二个字符
- SFML 碰撞永远不会在我的系统中注册
- 先进先出:一个进程永远不会从管道读取
- 移动构造函数永远不会被调用
- 程序永远不会进入虚拟析构函数
- 在 *.cpp 文件中实现的 c++ 函数/方法永远不会内联扩展吗?
- 指针永远不会在链表深层复制构造函数中达到 null
- 为什么我的 while 循环永远不会在 C++ 中的数字猜测游戏中结束?
- 类似函数的宏参数名称替换是否保证永远不会发生?
- curl_slist->next 永远不会为空,因此当循环卡住时
- 我应该使用无符号字符而不是 int 来存储永远不会大于 255 的值吗?
- cudaMemcpyToSymbol只是挂起,永远不会返回.GPU 处理速度为 100%.代码在 K40 上工作正常,但
- 暂停功能,它永远循环
- 字符串迭代器永远不会达到 std::string::end()
- C++多线程应用程序将永远挂起
- Linux 从永远在线的应用程序中创建和删除文件
- 为什么我的线程永远不会结束
- 无法从串行端口读取.WaitCommEvent() 永远不会返回