问题标签 [destruction]

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 投票
1 回答
351 浏览

c++ - 尝试销毁 sf::Font 时出现分段错误

我正在使用 SFML 包并在尝试关闭窗口/程序时收到分段错误。我在我的代码中找到了导致分段错误的行,这是当我尝试破坏sf::Font用于在 SFML 窗口中绘制文本的时候。

这条线很简单:

delete button_font;(这是类型sf::Font*

我从 SFML 文档中读到的,sf::Font该类确实有一个析构函数。我还在定义字体对象后立即对其进行了测试,它按预期自行破坏。

字体由sf::Font::loadFromFile(~). 在某些用作游戏状态和菜单的类中,它被用作引用参数。sf::Text当我尝试删除字体时,它被多个对象使用可能是一个问题吗?

0 投票
3 回答
1384 浏览

javascript - 使用值解构声明错误

我不明白为什么解构赋值后,itemsprop 不等于Gorilla

将在删除items: "Piggi"原始对象选项中的主要道具后使用。我不明白为什么...

0 投票
2 回答
187 浏览

class - 销毁后无法在 AHK 中创建类

我正试图围绕 AHK 的课程。我是 C++ 开发人员,所以想使用 RAII ( __New, __Delete),但看起来我错过了一些概念,因为事情对我来说看起来非常违反直觉。

经过一些尝试,我想出了这个简单的例子:

结果,我收到以下消息:

  1. 新的
  2. 新的
  3. 删除
  4. 删除

问题:

  1. 为什么在第二次分配期间对象没有被销毁?我假设裁判的数量将变为 0,不是吗?
  2. 我怎么会连续2次破坏?同时该对象存储在哪里?scenario变量如何同时保存两个引用?
  3. 为什么不调用第三个结构?
0 投票
3 回答
574 浏览

c++ - Late destruction of function parameters

According to 5.2.2/4 "Function call" in n4640 (8.2.2/4 in n4659) function parameters are created and destroyed in the context of the caller. And implementations are allowed to delay the destruction of function parameters to the end of the enclosing full expression (as an implementation-defined feature). Note that the choice is not unspecified, but rather implementation-defined.

(It is not entirely clear how this agrees with 3.3.3 "Block scope" (6.3.3 in n4659), which seems to imply that function parameters have block scope, and then 3.7.3 "Automatic storage duration" (6.7.3 in n4659), which says that the storage for block scope variables lasts until the block in which they are created exits. But let's assume that I'm missing/misunderstanding something in the wording. Apparently now function parameters will have their own scope)

As far as I know, ABI requires GCC and Clang to delay the destruction of function parameters to the end of full expression, i.e. this is the implementation-defined behavior of these compilers. I would guess that in implementations like that it should be OK to return references/pointers to function parameters as long as these references/pointers are used within the calling expression only.

However, the following example segfaults in GCC and works fine in Clang

Both compilers issue a warning about returning a reference to a local variable, which is perfectly appropriate here. A quick inspection of the generated code shows that both compilers do indeed delay the destruction of the parameter to the end of the expression. However, GCC still deliberately returns a "null reference" from foo, which causes the crash. Meanwhile, Clang behaves "as expected", returning a reference to its parameter s, which survives long enough to produce the expected output.

(GCC is easy to fool in this case by simply doing

which fixes the segfault under GCC.)

Is GCC's behavior justified in this case, under assumption that it guarantees "late" destruction of parameters? Am I missing some other passage in the standard that says that returning references to function parameters is always undefined, even if their lifetimes are extended by the implementation?

0 投票
0 回答
43 浏览

c# - 基于在不同类中调用的文件的主函数循环

这里是新人,所以请多多关照!!当谷歌让我失望时,我在学习编程时获得了很多乐趣,并在此过程中得到了一些很大的帮助。但很可惜,我又被困住了。

我有一个看起来像这样的 C# 程序(如果有人熟悉,那就是 MWS)

我已经尝试了很多不同的方法来让它有效地循环遍历文本文件中的值列表。我遇到的问题是 Main 函数是我必须设置循环的地方,但 BuildClass 是我需要循环遍历文本文件(哨兵)中的值的地方。我已经包含了一些可能没有必要的东西,以防万一它弄乱了我的代码而我没有意识到。

  • 这是我尝试过的:

    1. 在 BuildClass 中设置循环 - 没想到它会起作用,但它在到达哨兵之前抛出了异常。
    2. 通过将主函数哨兵中的“使用”或“变量”更改为公共来引用主函数中的哨兵 - 在视觉工作室中变成红色
    3. 将字符串 sentinel 移到主函数之外,以便函数和 BuildClass 能够识别它 - 主函数不再识别它。
    4. 我尝试了很多其他事情都没有成功。我已经让它循环使用从 BuildClass 传递给函数的相同标记值,但仅此而已。
  • 我认为我需要什么:

    1. streamReader 的破坏性版本,它会在读取文本文件时从文本文件中删除值。我将这个放在BuildClass里面,这样主函数的下一个循环,下一个值会被读取并传入主函数,直到文件为空,终止循环。
    2. 了解为什么将哨兵更改为公开会如此严重地破坏代码。我对为什么其他尝试不起作用有一个很好的理解。

0 投票
2 回答
165 浏览

c++ - 当左值分配给右值引用时会发生什么?没有销毁临时对象?

我的 Eclipse 结果:

我的期望:

我对这一步感到困惑。

当a被赋值给b时,它可以假设首先破坏b持有的临时对象(使用cstring“B代表Banana,rvalue ref”),然后将a的值分配给b。为什么在 Eclipse 的结果中,它不执行临时对象的销毁?

0 投票
1 回答
2368 浏览

java - Spring 自定义范围生命周期 Bean 终止

问题:我如何告诉 Spring 一组具有自定义范围的 bean 都应该被视为垃圾,以便同一线程上的下一个请求不会重用它们的状态?

我所做的:我在 Spring 中实现了一个自定义范围,以模拟请求范围 (HttpRequest) 的生命周期,但用于 TcpRequests。它与这里发现的非常相似。

我发现的自定义范围的许多示例是原型或单例的变体,没有显式终止 bean,或者,它们基于本地线程或 ThreadScope,但它们没有描述告诉 Spring 生命周期已经结束并且所有豆类应该被销毁。

我尝试过的事情(可能不正确):

  • Event + Listener 指示范围的开始和结束(这些发生在收到消息时和发送响应之前);在侦听器中,显式清除了范围,从而清除了线程本地实现(scope.clear())使用的整个映射。在测试中手动处理时,清除范围确实会导致对 context.getBean() 的下一次调用返回一个新实例,但是在单例类中自动装配的我的 bean 没有获得新的 bean——它一遍又一遍地使用同一个 bean .

  • 实现的监听器:BeanFactoryPostProcessor、BeanPostProcessor、BeanFactoryAware、DisposableBean 并尝试在所有 Disposable bean 实例上调用 destroy();像这样的东西,但仅适用于我的自定义范围。这似乎失败了,尽管我在收到范围结束事件时调用了 customScope.clear() ,但没有任何东西明确结束 bean 的生命周期;结束范围似乎并不意味着“结束与此范围关联的所有 bean”。

  • 我已经广泛阅读了 Spring 文档,似乎很清楚 Spring 不管理这些自定义 bean 的生命周期,因为它不知道何时或如何销毁它们,这意味着必须告诉它何时以及如何销毁摧毁他们;我试图阅读和理解 Spring 提供的 Session 和 Request 范围,以便我可以模仿这一点,但遗漏了一些东西(同样,这些对我来说不可用,因为这不是一个支持 Web 的应用程序,而且我不是使用 HttpRequests,这是对我们应用程序结构的重大更改)

有没有人能指出我正确的方向?

我有以下代码示例:

XML 上下文配置

TransactionController(处理请求)

TcpRequestScope(范围定义)

TcpRequestCompleteListener

RequestWrapper(我们在整个请求生命周期中使用的对象)

0 投票
1 回答
956 浏览

javascript - eslint 标记的数组销毁和未使用的变量

我的代码中有这样的代码:

变量b, c,e在下面的代码中使用但不是aand d。同时我有 eslint 检查标记未使用的变量。

有没有办法更正确地编写破坏来解决这个问题?我知道esling-disable-line no-unused但更愿意避免它。

0 投票
3 回答
232 浏览

c++ - 如何从指向指针数组的指针中删除对象?

我对 C++ 中的手动销毁仍然很陌生(来自带有垃圾收集的语言)。我的一门课有以下内容:

Input** Inputs;

初始化如下:

this->Inputs = new Input* [totalInputs];

并且可以根据用户输入在我的代码中重新分配,类似于:

this->Inputs[inputNumber] = new DigitalInput(params...)

这样做的问题是,由于释放旧对象,在该位置重新分配对象时,它容易发生内存泄漏。

重新分配旧对象后删除旧对象的最佳方法是什么?

编辑:我忘了包括这是在 Arduino 代码库上运行的 AVR 微控制器上。

编辑2:我这样做的原因是因为允许用户向将更改输入类型的单元发送命令(即:发送命令和this->Inputs[inputNumber] = new AnalogInput(params...)。也是指向指针数组的指针的原因是因为该对象的构造函数将根据totalInputs传入的参数生成该数组。这是在一个共享库中,用于几个不同的单元。

0 投票
1 回答
26 浏览

java - 我们初始化我们的主对象,并在其构造函数中创建一个新对象。当我们销毁主要对象时,它的创建会发生什么?

我认为标题非常具体,但这里有一些代码来举例说明这个问题。此外,我意识到聚合将是这个特定示例的正确选择,而且问题本身可能会带来 OOP 气味;然而,虽然我对 OOP 感兴趣,但我更感兴趣的是 Java 如何处理这里的对象销毁。

和跑步。

这里发生了什么,没有其他引用存在(这可能无关紧要,不确定何时写):