奇怪的记忆泄漏问题 [英] Strange MemoryLeak Issue

查看:68
本文介绍了奇怪的记忆泄漏问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


我希望这属于正确的话题...


我们有一个使用COM端口的小应用程序在与外部设备通信的系统上。这个应用程序是用Delphi编写的,并使用了一些第三方组件。


我们有大约500个相同的系统,有点说话,而且这个应用程序只有一个位置正在获得高达500mb的记忆。

所有其他地点都运行良好,不会遇到这个问题。


我更倾向于相信这是该系统上的Windows问题,然后是应用程序中的错误。谁可以对此有所了解,或者遇到过类似的问题?

Hello,

I hope this falls under the correct topic...

We have a small application that uses the COM ports on a system to communicate with external devices. This application is written in Delphi and makes use of some third-party components.

We have around 500 identical systems, sort of speak, and there''s only one location where this application is getting memoryleaks up to 500mb.
All other locations are running fine and don''t suffer from this problem.

I''m more inclined to believe this to be a Windows problem on that system then a fault in the application. Anyone who can shed some insight on this, or experienced a similar issue?

推荐答案

在大多数Delphi应用程序中,你使用放在表单上的组件,你可以不需要太在意内存管理。将组件放置在表单上后,表单将成为其所有者,并在表单关闭后释放组件占用的内存。作为所有者,Form负责其托管的组件的内存释放。简而言之:表单上的组件会自动创建和销毁。现在,当您需要使用TDeveloper类时,可以通过调用Create方法(构造函数)来创建类的实例。 Create方法为新对象分配内存并返回对象的引用。


var

zarko:TDeveloper

begin

zarko:= TMyObject.Create;

zarko.DoProgram;

end;


这个是一个简单的内存泄漏
In most Delphi applications, where you use the components you drop on a form, you do not need to care to much about memory management. Once the component is placed on a form, the form becomes its owner and will free the memory taken by the component once the form is closed. Form, as the owner, is responsible for memory deallocation of the components it hosted. In short: components on a form are created and destroyed automatically. Now, when you need to use the TDeveloper class, you create an instance of the class by calling the Create method (constructor). The Create method allocates memory for a new object and returns a reference to the object.

var
zarko : TDeveloper
begin
zarko := TMyObject.Create;
zarko.DoProgram;
end;

This is a simple memory leak


每当你创建一个对象时,你必须处置它占用的内存。要释放内存分配的对象,必须调用Free方法。为了完全确定,你还应该使用try / finally块:


var

zarko:TDeveloper

begin

zarko:= TMyObject.Create;

尝试

zarko.DoProgram;

终于

zarko.Free;

end;

end;


这是安全内存分配和释放代码的示例。


警告:如果要动态实例化Delphi组件并在以后显式释放它,请始终将nil作为所有者传递。如果不这样做可能会带来不必要的风险,以及性能和代码维护问题。
Whenever you create an object, you must dispose the memory it occupied. To free the memory an object allocated, you must call the Free method. To be perfectly sure, you should also use the try / finally block:

var
zarko : TDeveloper
begin
zarko := TMyObject.Create;
try
zarko.DoProgram;
finally
zarko.Free;
end;
end;

This is an example of a safe memory allocation and deallocation code.

Warning: If you want to dynamically instantiate a Delphi component and explicitly free it sometime later, always pass nil as the owner. Failure to do so can introduce unnecessary risk, as well as performance and code maintenance problems.


我知道Delphi中可能导致内存泄漏的原因。
我'我更感兴趣的是为什么内存泄漏只发生在一个特定的系统而不是我们运行的245个其他系统上。


他们都在运行相同的操作系统,相同的硬件和运行相同版本的软件。
I know what might cause a memory leak in Delphi.
I''m more interested as to why the memory leak only occurs on one specific system and not the 245 other systems we have running.

And they''re all running the same OS, have the same hardware and run the same version of the software written.


这篇关于奇怪的记忆泄漏问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆