问题标签 [qshareddata]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
2 回答
849 浏览

c++ - 具有前向声明类的 QSharedDataPointer

Qt 文档建议将QSharedDataPointer 与其下级的可见实现一起使用是不典型的

所以根据文档中的小例子,我想出了以下来源(SSCCE)。

接口:Model.h

接口很简单,只是私有类和句柄类的前向声明,声明了copy-ctor和d-tor:

私有标头:Model_p.h

只需声明并定义低级类。

实现:Model.cc

包含 c-tors/d-tor 的实现,取自文档。

用例:main.cc

这一切都失败了。

只有两个实例和一个作业,就像对任何其他共享类一样。但是,它失败了,因为

根据文档,我无法弄清楚如何使这项工作以预期的方式工作,即没有在标题中完全声明私有类。我知道这样做会起作用,但我想了解文档。分配共享类的示例也包含在文档中。从上面链接的文档中:

这里并不严格要求复制构造函数,因为 EmployeeData 类与 Employee 类 (employee.h) 包含在同一个文件中。但是,在与包含 QSharedDataPointer 的公共类相同的文件中包含 QSharedData 的私有子类并不典型。通常,这个想法是通过将 QSharedData 的私有子类放在一个不会包含在公共文件中的单独文件中来对用户隐藏它。在这种情况下,我们通常会将 EmployeeData 类放在一个单独的文件中,该文件不会包含在 employee.h 中。相反,我们只需以这种方式在 employee.h 中预先声明私有子类 EmployeeData:

我想operator=在分配Model.

0 投票
1 回答
344 浏览

c++ - Confusing about implicit sharing concept in Qt

According to implicit sharing concept in following example we must experience low memory usage in Windows Task Manager.

We create 1000000 object in a for loop from Employee class and it might share its internal data (EmployeeData class) between created objects.

***: if uncomment mentioned line according to Qt documentation QSharedDataPointer it will be detaches from shared data and creates its own copy of EmployeeData.

EmployeeData and Employee class:

Platform: Windows 10

Qt Version: 5.7

Compiler: MSVC 2015 32bit

The result is (from Task Manager):

  • Release: 40M RAM usage
  • Debug: 189M RAM usage

Question:

Based on provided loop assume EmployeeData size as 12 bytes, it must create one instance of EmployeeData and share it with another 999,999 object instances, and so memory usage must be reduced at least under 3M, is it Ok?

So if we uncomment the following line it must create 1000,000 unique instance of EmployeeData, so memory used by instances goes up, is that Ok?

0 投票
0 回答
84 浏览

c++ - 我是否明确需要在 QSharedData 的继承类中声明析构函数?

我的问题是我是否明确需要在我的主类和 QSharedData 继承的类中声明一个析构函数,以防止内存泄漏。在此示例中,我有一个 Employee 类,它通过 EmployeeData 处理其数据。

所以我需要声明

为了防止内存泄漏或它由 QSharedData 本身处理,即使没有显式析构函数,它也会在 Employee 超出范围时释放其所有资源?

这是一个正在实施的示例:

提前致谢

0 投票
0 回答
27 浏览

c++ - QT qSharedDataPointer 动态转换?

我正在尝试执行以下演员,但我无法在 QT 文档或在线找到一种方法来使这种令人困惑的动态演员工作:

以上产生错误:

我错过了什么?