问题标签 [resource-management]

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 回答
248 浏览

asp.net - 在 ASP MVC 中有一个外部资源 dll 没有意义吗?

我一直在尝试在 ASP.NET MVC 应用程序中找到推荐的资源管理实践。(目标 .NET 4.0)

到目前为止,我还没有弄清楚是否应该将单独的附属程序集添加到我的 MVC 应用程序或在项目本身中添加资源。

此外,MVC 应用程序中的卫星程序集创建也有很大不同。也许它们甚至不被称为附属程序集。我的理解是,为了引用外部 resource.dll 文件,我应该创建一个不同的 ASP.NET MVC 项目,将我的资源文件添加到“资源”文件夹和然后添加对由此生成的 dll 的引用。这是正确的方法吗?我认为 al.exe 进程与 MVC 应用程序无关。

0 投票
2 回答
1494 浏览

c++ - 用于函数对和模板专业化的 RAII 包装器

我为初始化和释放资源的 C 函数对编写了一个 RAII 包装器,它在大多数情况下都能很好地为我服务。

但是,比如说当一个函数返回时,voidEnter/LeaveCriticalSection不确定如何在这个类中进行操作。我应该专门针对SuccessValueType = void案例的课程吗?或者具有默认模板参数的东西应该做什么?

0 投票
2 回答
245 浏览

scala - 在不维护可变状态的情况下释放 Scala 中的 IO 资源

我需要使用一些 Java 库,它可能会在一个方法中抛出一些异常,并在另一组方法中返回错误代码。到目前为止,它导致了丑陋的代码,例如

我最感兴趣的是摆脱状态var和在 finally 块中检查状态的能力。请指教。

0 投票
5 回答
2314 浏览

c# - 不会被释放的静态变量

我的班级中有一个static StreamWriter变量:

我没有在我的应用程序中关闭此 StreamWriter,因为它需要在应用程序运行时打开。

如果我多次启动和停止此应用程序,我会出现内存泄漏吗?还是在关闭应用程序时正确处理了对象?

此类是 ASP.NET MVC 4 和 WPF 应用程序都使用的实用程序类。

感谢大家的回复。这是我添加的代码:

在包含 StreamWriter 的类中:

在 ASP.NET MVC Global.Asax 中:

0 投票
1 回答
1647 浏览

ruby - 如何确保在 Ruby 中关闭数据库连接?

我正在学习红宝石。我正在尝试使用mysqlgem 连接到 MySQL db。我关心的一个问题是连接关闭。

我应该如何确保在异常抛出等意外情况下关闭连接?

0 投票
1 回答
81 浏览

xml - 存储配置的最佳方式

我正在实现一个具有完整 API 的配置管理器,类似于 Windows 注册表。我希望它是人类可读的,因为能够使用文本编辑器打开和编辑它。我希望它能够存储程序可能需要的任何类型的配置信息,并且速度足够快以备不时之需。

我查看了以下用于存储信息的标记语言:

  • XML
  • JSON
  • YAML
  • S-表达

但我不确定哪种语言适合这项任务。

0 投票
2 回答
72 浏览

java - 手动打开 Java 7 自动管理的资源

所以 Java 7 有一个漂亮的特性可以自动关闭一个AutoCloseable类。如果我有Bar实现Closeable(这反过来扩展AutoCloseable),并且有办法Bar从 a打开Foo,我可以这样做:

...并且bar会自动关闭,就像放在一个finally子句中一样。迷人的。

但是,如果我想获得一个Bar并稍后打开它怎么办?因为也许 aBar就像 aFile并且只是标识一个资源;我可能想识别其中很多,但只在必要时打开它们。

但是 Java 7 自动管理的方法要求我在try子句中分配一个变量,不是吗?

所以也许我可以很聪明并且有Bar.open()回报this(即Bar实例),这样我就可以做到这一点:

这个功能如我所愿,但它给了我一个警告,中间Bar实例永远不会关闭。

所以也许我可以这样做:

这也是我想要的功能,但除了丑陋之外,我仍然收到警告——这次第一个bar变量永远不会关闭。

所以也许我可以将bar.open()调用放在受保护的块内,如下所示:

这将按我想要的方式工作——大多数时候。但是如果bar.open()抛出异常怎么办?Java 7 将尝试关闭bar,在我的实现中,它会抛出一个,IllegalStateException因为您无法关闭从未打开过的东西。所以它应该,对吧?因为如果有人试图在Bar实例打开之前关闭它,就会出现问题,我们需要一种快速失败的方法,而不是让问题在未知时间传播并浮出水面。

但也许我真的很想使用自动资源管理,所以我考虑放松一下Bar.close(),这样你可以随时关闭它,即使你没有打开Bar. 但现在看看我在做什么:我正在改变我的 API(可以说让它变得低劣)只是为了使用一些语法编译器糖!

还有其他想法吗?我想使用 Java 7 自动资源管理,以便 Java 7 自动关闭我的资源,但想决定何时打开它,这可能不一定是在我获取资源的那一刻。

0 投票
1 回答
167 浏览

c++ - 将 C 函数包装在自动对象中,无需重复代码

在 C++03 中,当您将一堆 C 函数包装在一个类中以创建“自动对象”时,您必须将对象自定义为它封装的函数类型。例如,要包装 Windows 文件 HANDLE,您需要在析构函数中调用CloseHandle() ,在构造函数中调用CreateFile()。构造函数需要模仿 CreateFile() 函数的函数签名,没有文件 HANDLE 变量(因为它正在被管理)。

无论如何,我想知道的是,是否可以使用 C++11 的新特性来创建一个通用类,该类可用于包装任何类型的资源,只提供创建和删除的实现?

我预见到的一个问题是创建函数,例如上面提到的 CreateFile(),可以采用任意数量的参数。有没有办法自动生成一个模仿函数签名的模板化构造函数?想到可变参数,但我还没有使用它们。

有没有人试过写这样的东西?

编辑:一些代码来帮助说明(伪):

然后这将是我的超级自动对象的实现:

shared_ptr是的,这与or具有类似的结构unique_ptr,但构造函数将是由开发人员编写的创建者和删除器类创建资源的构造函数。我有一种感觉 std::bind 可能在其中发挥作用,但我从未使用过它。

0 投票
1 回答
155 浏览

c++ - 实现这个的更好/内存效率更高的方法是什么

假设我有一个Font看起来像这样的类:

我有一堂课Label

然后对于所有这些我担心,如您所见,该BMfont课程使用大量资源。我将把类继承SWC_Label给一个类SWC_Button(是的,一个按钮,上面有标签/文本)。

现在,我希望它SWC_Button具有具有不同字体的功能。做这种事情的更好和内存效率更高的方法是什么,我应该做一个限制,比如:只制作定义数量的可用字体(在类标签中制作静态字体)?

注意:我正在使用 OpenGL 制作 UI

0 投票
1 回答
1455 浏览

android - Android splashscreen without creating a new activity

Edit

After moving my loading / creation code to an Async Task (see below) - I still have the initial problems that I had with my original splashscreen.

Those being that:

1) On starting the Async task in onCreate, everything is loaded but my Dialog can only be shown when onStart() is called which makes the whole process kind of pointless as there is a long pause with a blank screen, then after everything has loaded, the 'loading' dialog flashes up for a split second before disappearing.

2) I can't move object loading / creation etc to onStart because a) it will be run again even when the app is resumed after being sent to the background which I don't want to happen, and b) when when calling restoring the savedInstanceState in onCreate() I'll get a nullPointerException because i'm restoring properties of objects that won't have yet been created.

Would really appreciate if someone could advise how to get around these problems and create a simple splashscreen. Thanks!

Background

My app uses only one activity and I would like to keep it that way if possible.

I've struggled with this for over a week so really hope someone can help me out.

All I want to do is use a splashscreen with a simple 'loading' message displayed on the screen while my resources load (and objects are created etc.) There are a couple of points:

Conditions

1) The splashscreen should not have it's own activity - everything needs to be contained in a single-activity

2) The splashscreen should not use an XML layout (I have created a Splashscreen class which uses View to display a loading PNG)

3) My app is openGL ES 2.0 so the textures need to be loaded on the OpenGL Thread (creation of objects etc that don't use GL calls are OK to go on another thread if necessary).

What I've attempted so far

What I did so far was to create a dialog and display it in my onStart() method with:

then let everything load in my onSurfaceCreated method before getting rid of it with:

However I needed to change this for varioius reasons so now I create my objects from a call within my onCreate() method and just let the textures load in my GL Renderer's onSurfaceCreated method.

However, this means that because the dialogue isn't displayed until after onCreate, I still get a delay (blank screen) while everything is created before the splash-screen is shown, this then stays on the screen until the textures have loaded. There are other issues with this too which can wait for another day!

So my approach is obviouly very wrong. I read every tutorial I could and every splash-screen related question I could find on SO and Gamedev.SE but I still can't find an explanation (that makes sense to me), of how this can be achieved.

I'm hope someone here can explain.