class AutoSomething
{
public:
AutoSomething(Object& ob)
: object_(object)
{}
~AutoSomething()
{
object_.some_callback();
}
private:
Object& object_;
};
.........
void Object::some_function()
{
AutoSomething some(*this);
some_function_which_may_throw_exception();
}
Run Code Online (Sandbox Code Playgroud)
问题是 - 当AutoSomething的析构函数被调用时,Object的状态是否正常?
我看过几个类似的代码片段,如下所示:
struct MyExcept : std::exception {
explicit MyExcept(const char* m) noexcept : message{m} {}
const char* what() const noexcept override {
return message;
}
const char* message;
};
void foo() {
std::string error;
error += "Some";
error += " Error";
throw MyExcept{error.c_str()};
}
int main() {
try {
foo();
} catch (const MyExcept& e) {
// Is this okay?
std::cout << e.message << std::endl;
}
}
Run Code Online (Sandbox Code Playgroud)
在注释后面的行中Is this okay?,我们读取了在foo函数中使用分配的c样式字符串std::string.由于字符串是通过堆栈展开来破坏的,这种未定义的行为是什么?
如果它确实是未定义的行为,如果我们main用这个替换函数怎么办?
int main() …Run Code Online (Sandbox Code Playgroud) 在C ++中,我如何在析构函数的主体中检测是否由于抛出异常而取消了堆栈的堆栈?一旦检测到,是否可以引用活动异常?
我之所以问是因为我想添加一些调试代码来解释为什么可能会出现某种情况以及它是否是由于异常引起的。
我有以下代码:
#include <iostream>
#include <vector>
#include <tr1/memory>
struct FooError {};
struct Foo
{
~Foo() { std::cerr << "~Foo() executed" << std::endl; }
explicit Foo(unsigned int index) { if (5 == index) throw FooError(index); };
};
int main() {
typedef std::tr1::shared_ptr<Foo> FooPtr;
std::vector<FooPtr> foos;
for (unsigned int index = 0; index < 20; ++index)
{
try
{
foos.push_back(FooPtr(new Foo(index)));
}
catch (const FooError&)
{
std::cerr << "FooError caught" << std::endl;
}
}
}
Run Code Online (Sandbox Code Playgroud)
~Foo()当有try{} catch{}块时我看到一组被执行.如果没有异常处理程序,则不会打印任何内容.是否意味着在处理异常时会调用堆栈分配对象的析构函数?或者因为std :: cerr缓冲问题而没有打印出来?
我有一个c ++代码,我用MSC9来编译它.它会随机崩溃.例如,如果从Perl使用``调用它会崩溃,但是当它从命令行或从Ultimate ++调用时它不会崩溃.
我的意思是从perl调用它,例如. f.exe arg1 arg2 arg3
堆栈跟踪显示不多.逐行跟踪程序证明程序在返回时失败了......
所以就是这样
int funcname()
{
return 0; <-- crashing after that...
}
Run Code Online (Sandbox Code Playgroud)
我猜堆栈已损坏,堆栈解除后,它崩溃了..
什么可以导致它?该程序使用pcre,stl和迭代器.迭代器可以打破堆栈吗?你怎么会遇到这样的错误?
它可以是编译器错误吗?
注意:调试版本不会崩溃,只会发布版本...
这个错误似乎与这个pvector类有关.
我有一个类似于这样的结构:
struct complexstr
{
pvector<int> v;
string v2;
hash_map<string> hm;
vector<string> vs; // similar
int i;
};
Run Code Online (Sandbox Code Playgroud)
它似乎失败了,因为这一行:
complexstr s1;
complexstr s2;
s2=s1; // it seems to fail here, if this is not there... there is no error.
Run Code Online (Sandbox Code Playgroud)
我认为问题出在下面的类... std :: copy在pvector operator =(const pvector&pv)中是正确的,对吧?
pvector是一个perl兼容的向量...它的索引可以大于向量的分配大小.
Update1:我收到了有关作业泄漏的建议.我改变了作业......现在看起来是这样的:
pvector& operator=(const pvector &pv)
{
delete [] m_rgArray; …Run Code Online (Sandbox Code Playgroud) 以下是否有任何陷阱;
if (someCondition)
throw boost::shared_ptr<SomeException>( new SomeException( "foo!" ) );
...
catch( const boost::shared_ptr<SomeException>& expRef )
{
}
Run Code Online (Sandbox Code Playgroud) 问:在堆栈展开时引发并捕获异常是安全的,还是应用程序terminate在第二次引发时调用?
最小的例子:
void some_function()
{
try
{
// do stuff here that can throw
throw std::runtime_error("blah");
} catch(const std::exception& re)
{
try // this code could be in some function called from here
{
// do something with re here that throws a logical_error
throw std::logical_error("blah blah"); // does this call terminate?
} catch(const std::logical_error& le)
{
}
}
}
Run Code Online (Sandbox Code Playgroud)
看完这个问题我很好奇。
注意:我知道您可以/应该catch(...)在析构函数中使用,但是通常try/catch在一个catch块中包含有意义-也许在调用异常的某些函数中(re在我的示例中)?
我在IDA的RUNTIME_FUNCTION结构的.pdata段中找到了一个大数组。因此,在哪里可以找到信息:从它的编译,如何创建以及如何在C ++中使用信息。请给我书籍,或提供具有良好描述和教程的链接,以使用此结构来处理异常和消除异常。
在我的studing exeption机制中,我发现在堆栈展开时会有对象字段的析构函数调用.让我明确解释一下:
class X
{
File_ptr aa;
Lock_ptr bb;
public:
X(const char* x,const char* y):aa(x),bb(y){}
//.......
}
Run Code Online (Sandbox Code Playgroud)
所以,现在如果Lock_ptr的构造函数抛出一个exeption,对象aa将被销毁; 问题是"为什么"?我一直认为对象的文件不是通常的自动(lochal)对象.它们是在构造函数初始化之前创建的.所以它们在超出构造函数范围之后就不能被破坏(否则它们会被破坏)构造函数完成了它的工作)
我有一个 C++ 测试程序,可以让 CPU 保持忙碌:
\n#include <cstdint>\n#include <iostream>\n\n// Linear-feedback shift register\nuint64_t lfsr1(uint64_t max_ix)\n{\n uint64_t start_state = 0xACE1u; /* Any nonzero start state will work. */\n uint64_t lfsr = start_state;\n uint64_t bit; /* Must be 16-bit to allow bit<<15 later in the code */\n\n for (uint64_t ix = 0; ix < max_ix; ++ix)\n { /* taps: 16 14 13 11; feedback polynomial: x^16 + x^14 + x^13 + x^11 + 1 */\n bit = ((lfsr >> 0) ^ (lfsr >> …Run Code Online (Sandbox Code Playgroud) 我已经阅读了几个答案和许多有关移动语义的文章,因此它只是对右值引用的静态转换,这个问题是关于它对堆栈的影响,那么移动后堆栈是否会留下碎片?还是以某种方式重新排列?因为堆栈移动的对象不受堆栈展开的影响。以下代码片段运行良好:
#include <memory>
#include <cassert>
struct A {
int x = 0;
};
struct B {
A a;
};
void SetB(B& b) {
A a1{6}; //pushing into the stack.
A a2{7}; //pushing into the stack.
b.a = std::move(a2);
}
int main()
{
B b;
SetB(b);
assert( b.a.x == 7);
}
Run Code Online (Sandbox Code Playgroud)
a2是在SetB堆栈帧中定义的,但在SetB完成并且控件返回到后仍然可用main。然而; a1不是尽管它更接近main堆栈帧吗?
好的,我通过2层函数fun1调用func2调用func3.我使用基本上int*ptr一直向下传递一个指针,在调用堆栈的最低"级别"我还有另一个为int数组动态分配内存的函数.在顶层(func1级别),我总是为传递的指针返回null.我已经追溯到func3并且分配的内存正在填充值,但是当调用堆栈突然解除func3 - > func2时,指针就会消失(0x0000_0000)?我不明白在func3级别我基本上说ptr = allocate_ptr_array,但从那个返回它变为NULL!即使我没有释放记忆,世界上还在发生什么?我知道我的问题令人困惑.我已经在调试器中看到过这种情况