Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What exactly is "handle"?

I've often heard about "handles", what exactly are those?

Edit: For instance I have heard about: windows handles event handles file handles

and so on. Are those things the same? Or they are some abstract terms?

like image 979
kofucii Avatar asked Sep 01 '10 17:09

kofucii


People also ask

What is a handle of an object?

The term handle is used to mean any technique that lets you get to another object — a generalized pseudo-pointer. The term is (intentionally) ambiguous and vague. Ambiguity is actually an asset in certain cases.

What is a handle C++?

In C++/CLI, a handle is a pointer to an object located on the GC heap. Creating an object on the (unmanaged) C++ heap is achieved using new and the result of a new expression is a "normal" pointer. A managed object is allocated on the GC (managed) heap with a gcnew expression. The result will be a handle.

Is a handle a pointer C++?

Handle pointers are instances of a type called a handle. You use a handle instead of an ordinary pointer to refer to a data structure. Handle pointers point to data in some external memory area, such as expanded memory, extended memory, or disk.

What is a handle in Windows H?

HANDLEs are generally prefixed with an "h". Handles are unsigned integers that Windows uses internally to keep track of objects in memory. Windows moves objects like memory blocks in memory to make room, if the object is moved in memory, the handles table is updated.


3 Answers

A handle is an indirect way to reference an object owned by the OS or a library. When the operating system or a library owns an object but wants to let a client refer to it, it can provide a reference to that object called a handle.

Handles can be implemented in different ways. Typically they are not references in the C++ or C# sense. Often they are pointers cast to some opaque type, or they might be (or contain) an index into a table of objects that are owned by the operating system or library.

For example, in Windows, if you create a window, the OS creates an object that represents the window, but it doesn't return a pointer to that object. Instead, it returns a window handle, which provides an extra layer of indirection. When you pass the window handle back in another OS call, the OS knows which window object to use based on the handle. This prevents your code from directly accessing the window object.

The extra layer of indirection allows the OS or library to do things like move objects around, reference count the objects, and generally control what happens to the object. Like the PIMPL idiom, the implementation may change completely while still preserving the original API and thus not forcing clients to recompile. It's especially useful if you're trying to offer a non-object-oriented API for clients written in procedural languages like C.

like image 72
Adrian McCarthy Avatar answered Oct 19 '22 12:10

Adrian McCarthy


A "handle" is another name for a reference to a resource which is managed by the programmer explicitly instead of automatically by the runtime.

like image 42
maerics Avatar answered Oct 19 '22 12:10

maerics


Handles are pointers to unmanaged resources like file handles, database connection handles, windows handles, etc... As they are handles to unmanaged resources in most cases they won't be automatically garbage collected and you need to ensure to properly release them or you might hear about leaking handles.

like image 22
Darin Dimitrov Avatar answered Oct 19 '22 12:10

Darin Dimitrov