From Gnash Project Wiki

Jump to: navigation, search

A weak pointer is a pointer to an optional referent. In an unmanaged environment, this is equivalent to an ordinary C pointer, where a zero value means that the pointer has no referent. In a resource-managed environment, however, the mere existence of an ordinary pointer (explicit or implicit) to a referent object forces the continued existence of that object. Therefore, an ordinary pointer in such an environment can always be dereferenced. A weak pointer in a managed environment, on the other hand, does not force the existence of its referent. Because existence of the referent does not depend upon a weak pointer to it (in general), dereferencing a weak pointer can result in "no such object". Weak pointers, in other words, are observers without being participants.

Weak pointers are useful both for correct operation of a program as well as to optimizing performance. A weak pointer is a widespread pattern when a subordinate object that always exists (such as an interface to a physical I/O device) has to make reference to a control object that may or may not exist. The subordinate object cannot force the existence of its control object, so it should hold a weak pointer to it. In the performance area, weak pointers are useful for minimizing memory usage. An object that can be reconstructed without loss of correctness, such as an object representing a database entry, cam be pointed to with a weak pointer, allowing that object to pass out of main memory without incident.


Use Cases in Gnash

  • Debugger.

A debugger is, by conception, an ordinarily unobtrusive observer of a running program. Implementing a debugging facility within a VM yields an opportunity to use weak pointers to refer to objects-under-test. Since the pointers are weak, their destructors will be called at the same point as they would be were the debugger not observing them.

  • Prototype members of function objects.

The constructor of a function object is the function itself, so creation of a function object instantly creates a circular reference. The following assertion is a postcondition to the creation of a new function object:

Function.prototype.constructor == Function

Now suppose that the constructor were initialized with a weak reference to itself. The weak reference won't force the existence of itself, but that's not necessary because we can presume that the function exists for the lifetime of all of its properties. (Think about that again if it wasn't crystal clear the first time.) The constructor property can be assigned to. In the case of an ordinary assignment to a new unrelated function (which itself has a different self-reference by default), the weak reference is replace by an ordinary strong reference, so the constructor reference remains valid. The advantage, however, of a weak reference for the initial constructor is that it will work correctly with a reference counting mechanism rather than requiring all the graph traversal-and-marking of a garbage collection algorithm.

Implementation in Different Environments

In a resource counted (RC) environment, the creation of a weak pointer does something quite simple: it doesn't increment the reference count for the referent object. Now, a typical operation available to a weak pointer is to strengthen it. Thus, a weak pointer has much of the structure of a strong, but behaves differently with respect to the reference count.

In a garbage collected (GC) environment, a weak pointer has a somewhat more sophisticated interpretation. Viewing objects and their contained pointers as vertices and edges in a connectivity graph, GC works by determine graph connectivity from some set of rooted objects (which are required to be always present). A weak pointer in a GC environment does not generate an edge in the connectivity graph, at least not for the purposes of determining reachability.

Other Implementations

The Boost types shared_ptr and weak_ptr are a simple example of a managed memory environment. shared_ptr implements a reference-counting strategy. A weak_ptr may point to the same object as a shared_ptr. There is a good list of smart pointer programming techniques there.


This page is about MemoryManagement in Gnash.