标签: object-lifetime

C++迭代器的生命周期和有效性是什么?

我打算在C++中实现一个Things列表,其中的元素可能会被无序删除.我不希望我需要任何类型的随机访问(我只需要定期扫描列表),项目的顺序也不重要.

所以我想std::list<Thing*> with this->position = insert(lst.end(), thing)应该做的伎俩.我希望Thing类能够记住每个实例的位置,以便以后可以轻松地完成lst.erase(this->position).

但是,我对C++ STL容器仍然有点新意,我不知道将迭代器保持这么长时间是否安全.特别是,考虑到在插入Thing之前和之后将删除其他元素.

c++ iterator stl object-lifetime

11
推荐指数
1
解决办法
4161
查看次数

在调用析构函数之前,对象的生命周期结束了吗?

我不明白这个:

3.8/1"类型T的对象的生命周期在以下情况下结束: - 如果T是具有非平凡析构函数(12.4)的类类型,则析构函数调用开始,或者 - 对象占用的存储器被重用或释放. "

如果生命周期在析构函数启动之前结束,那是不是意味着访问析构函数中的成员是未定义的行为?

我也看到了这句话:

12.7"对于具有非平凡析构函数的对象,在析构函数完成执行后引用该对象的任何非静态成员或基类会导致未定义的行为."

但它并不清楚析构函数中允许的内容.

c++ destructor object-lifetime language-lawyer

11
推荐指数
3
解决办法
2126
查看次数

为什么PerThreadLifetimeManager在此示例中使用?

我按照下面链接的示例来设置统一以使用我的服务层.我的项目设置与本文中的项目非常相似,我理解除了为什么在注册服务依赖项时使用PerThreadLifetimeManager的所有内容.请记住,我也在使用我的服务层中使用的通用存储库和单元工作.大多数统一示例使用默认(瞬态)生命周期管理器,因为我的设置类似于下面的设置,我想知道为什么我应该使用PerThreadLifeimeManager?我正在为我当前的表示层使用ASP.NET Web表单项目,如果它发生了任何变化.

container.RegisterType<ICatalogService, CatalogService>(new PerThreadLifetimeManager())
Run Code Online (Sandbox Code Playgroud)

在asp.net MVC 3中使用EF代码第一个依赖注入的存储库模式

dependency-injection unity-container unit-of-work object-lifetime repository-pattern

11
推荐指数
1
解决办法
5316
查看次数

为什么在返回字符串的函数上调用std :: string.c_str()不起作用?

我有以下代码:

std::string getString() {
    std::string str("hello");
    return str;
}

int main() {
    const char* cStr = getString().c_str();
    std::cout << cStr << std::endl; // this prints garbage
}
Run Code Online (Sandbox Code Playgroud)

我以为会发生的是,getString()将返回一个复制str(getString()按价值计算收益); 因此,副本str将保持"活着" main()直到main()返回.这将cStr指向一个有效的内存位置:返回的副本的底层char[]char*(或其他)保留在哪里.strgetString()main()

但是,显然并非如此,因为程序输出垃圾.那么,问题是,何时被str销毁,为什么?

c++ string object-lifetime

11
推荐指数
2
解决办法
4471
查看次数

Qt对象的生命周期

Qt对象的生命周期是什么时候?

如:

QTcpSocket *socket=new QTcpSocket();
Run Code Online (Sandbox Code Playgroud)

什么时候套接字会被破坏?我应该用吗?

delete socket;
Run Code Online (Sandbox Code Playgroud)

有什么区别:

QTcpSocket socket;
Run Code Online (Sandbox Code Playgroud)

我无法找到关于此的深刻信息,欢迎任何评论或链接.

c++ qt object object-lifetime

10
推荐指数
2
解决办法
5522
查看次数

延长临时生命周期,使用块作用域聚合,但不能通过`new`; 为什么?

注意:这个问题本来是问,评论 瑞安海宁这个答案.


struct A { std::string const& ref; };
Run Code Online (Sandbox Code Playgroud)

// (1)

A a { "hello world" };              // temporary's lifetime is extended to that of `a`
std::cout << a.ref << std::endl;    // safe
Run Code Online (Sandbox Code Playgroud)

// (2)

A * ptr = new A { "hello world" };  // lifetime of temporary not extended?
std::cout << ptr->ref << std::endl; // UB: dangling reference
Run Code Online (Sandbox Code Playgroud)


  • 为什么临时的生命期延长到(1),而不是(2)

c++ object-lifetime language-lawyer c++11

10
推荐指数
1
解决办法
181
查看次数

C++:对象和外部函数的生命周期

假设我想调用我的对象的外部函数来在body构造函数中执行一些检查.由于对象的生命周期是在构造函数的主体完成执行时开始的,这是不安全的设计吗?

struct A;

void check(A const&) { /* */ }

struct A
{
    A() { check(*this); }
};
Run Code Online (Sandbox Code Playgroud)

我的意思是,我正在使用一个尚未存在的对象调用外部函数.是不确定的行为?

相关问题:如果我将该检查函数作为成员函数(静态与否),该标准在构造函数之外但在类中使用非实时对象的说法是什么?

在一个类的观点和它的用户(一种在课堂上和在课堂外的生活中)之间的终身概念有什么不同?

c++ constructor object-lifetime language-lawyer

10
推荐指数
1
解决办法
211
查看次数

const引用是否绑定到另一个引用,该引用是从临时的悬空引用转换而来的?

以下是代码段:

#include <iostream>
using namespace std;
struct B{
     int b;
     ~B(){cout <<"destruct B" << endl;}
};
B func(){
    B b;
    b.b = 1;
    return b;
}
int main(){
    const B& instance = (const B&)func(); //is `instance` a dangling reference?
    cout <<instance.b<<endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这个在线编译器中的输出是

destruct B
destruct B
1
Run Code Online (Sandbox Code Playgroud)

因此返回值似乎比cout操作早破坏.所以这instance似乎是一个悬垂的参考.

如果我们const B& instance = (const B&)func();改为 const B& instance =func();,那么结果是

destruct B
1
destruct B
Run Code Online (Sandbox Code Playgroud)

作为补充,如果我在vs2015中测试代码,那么输出是最后一个.但是,如果在gcc(4.6之前)中进行测试,则输出为前者,但后者为4.6之后的版本.所以我想知道在线编译器是错误的还是引用实际上是悬空的.

c++ object-lifetime language-lawyer reference-binding

10
推荐指数
1
解决办法
189
查看次数

谁拥有.NET中的包裹流(例如TextWriter)?

我最近遇到一个错误"ObjectDisposedException:无法访问已关闭的流"

[ObjectDisposedException: Cannot access a closed Stream.]
    System.IO.MemoryStream.Write(Byte[] buffer, Int32 offset, Int32 count) +10184402
    System.Security.Cryptography.CryptoStream.FlushFinalBlock() +114
    System.Security.Cryptography.CryptoStream.Dispose(Boolean disposing) +48
Run Code Online (Sandbox Code Playgroud)

使用以下格式的代码时:

using (var stream = new MemoryStream())
{
    using (var hashStream = new CryptoStream(stream,
                                    new SHA256Managed(), CryptoStreamMode.Write))
    using (var writer = new TextWriter(hashStream))
    {
        writer.Write("something");
    }
    // ^-- Exception occurs on hashStream Dispose
    //     While naively I assumed that TextWriter.Dispose wouldn't touch the
    //     underlying stream(s).
    return stream.ToArray();
}
Run Code Online (Sandbox Code Playgroud)

因此引发异常是因为TextWriter的Dispose释放了包装的Stream(hashStream).我的问题是这样的:

  1. 是否将此约定(使用默认构造函数/参数)应用于.NET中的所有流?

    是否有佳能讨论这种资源使用模式?例如,可以假设CryptoStream会关闭MemoryStream吗?我知道答案,并且还有其他问题,但如果有这样的话,我希望它在设计指南方面得到解决.

  2. 这种行为记录在哪里?

    我找不到TextWriter(stream)或者在CryptoStream …

.net c# stream object-lifetime objectdisposedexception

9
推荐指数
2
解决办法
398
查看次数

构造函数中的const int ref可以安全地绑定到文字吗?

我知道标准有一个例外,关于延长temporaries的生命周期,基本上说在构造函数中绑定const引用不会延长生命周期,但这是否也适用于文字?例如:

class C {
    private:
        const int& ref;
    public:
        C(const int& in)
            : ref{in}
        { }
};
Run Code Online (Sandbox Code Playgroud)

如果我有一个函数返回这种类型的对象

C f() {
    C c(2);
    return c;
}
Run Code Online (Sandbox Code Playgroud)

c.ref如果我知道它与文字绑定,那么调用者的值是否未定义?

c++ reference temporary object-lifetime language-lawyer

9
推荐指数
1
解决办法
255
查看次数