Monday, January 21, 2008

How to use interface correctly?

A: IparentInterface;
B: IchildInterface;

This code will generate error:

B: = IchildInterface (A); ==> will generate expected error whenever we access to B. At the time we down-cast A, we do not have exception.

The correct code should be:

A.QueryInterface (IchildInterface, B);

So the lesson here: Always use QueryInterface when you want to access other inteface of the same object. Do not cast Interface except you really know what you do.

Delphi Memory Leak

I have a lession learn today when I work with delphi. We should avoid using Tlist to store object which implemented any interfaces.

We should use TinterfaceList instead of Tlist. The reason is: Tlist use pointer to reference to object. When we get an object and from that object we get one of its interfaces. This interface is not referenced by anyone (although Tlist use pointer to point to the object) .Therefor when go out of variable scope, this interface is freed automatically.

Hereunder is the sample explaining the lession.


InterfaceA = Iinterface
Procedure methodA;
End;


ClassA = class(TinterfaceObject, InterfaceA)
Procedure methodA;
End;


Var
A: TOBbject
List: Tlist;
Begin
List := Tlist.Create;
List.Add(A);
methodX;
methodY; ==> Exception occur here.
End.

Procedure methodX;
Var
X: InterfaceA;
Obj: ClassA;
Begin
Obj := Tlist.Items[0];
Obj.QueryInterface(InterfaceA, X); ==> Get interface here. Reference count increased to 1
End; ==> Interface will release reference count. Reference count = 0 ==> Obj is freed.

Procedure methodY;
Begin
Obj := Tlist.Items[0];
Obj.methodA; รง Exception because of object is freed.
End;


Hope this lession lean can help you avoid unexpected exception.

Does java have memory leak?

According to the IBM's artical, JAVA also has problems of memory leak event though they have its own memory garbage collector. The main reason is that: design is not good.

I. How memory garbage collector work?

The job of the garbage collector is to find objects that are no longer needed by an application and to remove them when they can no longer be accessed or referenced. The garbage collector starts at the root nodes, classes that persist throughout the life of a Java application, and sweeps though all of the nodes that are referenced. As it traverses the nodes, it keeps track of which objects are actively being referenced. Any classes that are no longer being referenced are then eligible to be garbage collected. The memory resources used by these objects can be returned to the Java virtual machine (JVM) when the objects are deleted.

II. How it leaks?

"An object is only counted as being unused when it is no longer referenced" ==> The garbage collector can not do its job if an object is referenced by others object for a very long time even though it is no longer needed. It can lead to a terrible problems: out of memory.

Example:

Object A = new Object; //A exists until program terminate.
During program life cycle, an object B is created and referenced by A
Because programmer believe in garbage collector, so they do not free memory manually,
but let garbage collector do it for him ==> B exist until A freed.
If program create many and many B for its life ==> all B will exist too ==> cause out of memory.

It is the obvious example of problem of memory leak. So we do not believe in Garbage Collector 100%. Let free object manually by assigned unused object to nil.


III.Preventing memory leaks

You can prevent memory leaks by watching for some common problems. Collection classes, such as hashtables and vectors, are common places to find the cause of a memory leak. This is particularly true if the class has been declared static and exists for the life of the application.

Another common problem occurs when you register a class as an event listener without bothering to unregister when the class is no longer needed. Also, many times member variables of a class that point to other classes simply need to be set to null at the appropriate time.

Hope i can help you in avoid memory leak in java.

Saturday, January 19, 2008

How to get the path and filename of executable file of current process (or running process)?

How to get the path + file name of the current process or the running process?
There are two way to this:

I will illustrate idea in delphi

1. You can call the API function: GetProcessImageFileName

Declare prototype:

interface
function GetProcessImageFileNameA(hProcess: THandle; lpImageFileName: LPSTR;
nSize: DWORD): DWORD; stdcall;
{$EXTERNALSYM GetProcessImageFileNameA}
function GetProcessImageFileNameW(hProcess: THANDLE; lpImageFileName: LPWSTR;
nSize: DWORD): DWORD; stdcall;
{$EXTERNALSYM GetProcessImageFileNameW}
function GetProcessImageFileName(hProcess: THANDLE; lpImageFileName: LPTSTR;
nSize: DWORD): DWORD; stdcall;
{$EXTERNALSYM GetProcessImageFileName}

implementation

function GetProcessImageFileNameA(hProcess: THandle; lpImageFileName: LPSTR;
nSize: DWORD): DWORD; stdcall; external 'Psapi.dll' name 'GetProcessImageFileNameA';
function GetProcessImageFileNameW(hProcess: THANDLE; lpImageFileName: LPWSTR;
nSize: DWORD): DWORD; stdcall; external 'Psapi.dll' name 'GetProcessImageFileNameW';
function GetProcessImageFileName(hProcess: THANDLE; lpImageFileName: LPTSTR;
nSize: DWORD): DWORD; stdcall; external 'Psapi.dll' name 'GetProcessImageFileNameA';


Calling function:

+ GetWindowThreadProcessId(WindowHandle, @processID); ==> get processid
+ processHandle := OpenProcess(PROCESS_TERMINATE or PROCESS_QUERY_INFORMATION,
False, processID); ==> Get process handler
+ GetProcessImageFileName(processHandle , bufferout, lenghtofbufferout)


2. The second way:

Call function GetModuleFileName with first parameter nil
+ GetModuleFileName(0, Buffer, 1024); <== Buffer contain the current path of current process.

Google