The invention relates to object-oriented programming languages. More particularly, the invention relates to automated memory management of server-based objects in multithreaded applications through the use of a reusable class library.
Object-oriented programming (OOP) is a class of programming languages that are based on the concept of an “object”. An object is a self-contained component designed to complete specified tasks in a program. Java, C#, and C++ are all examples of OOP languages.
OOP languages define classes of objects. A “class” is a prototype for an object, or a set of objects that have a common structure and behavior. An object, also referred to as an “instance”, is a specific representation of a class. Each instance of a class has particular attributes, which are called “instance variables”. The types and names of the instance variables are defined for each class, but the individual values of the instance variables are set and changed in the object.
Accessing data in an object-oriented database may be performed in some systems, such as C++, by the use of “pointers”. A pointer identifies the physical location of data within a block of memory. Obtaining the value that a pointer refers to is called dereferencing the pointer. The heap is the memory location from which newly created objects or classes are allocated. When an application has no further need for an object, the memory allocated for that object should be released for future use. In C++, pointers can be manually manipulated with functions such as AddReference and RemoveReference, which can be used to help identify when a pointer should “destroy” itself. Essentially an integer value assigned to the pointer gets incremented up and down as an object's pointer is passed around from one application to another until the integer value of the pointer becomes zero. When the value becomes zero, the pointer destroys itself and the memory location is no longer allocated for that object. With the use of pointers, reference counting and memory management are both performed manually through the programmer's code. The management of pointers can be time-consuming during development and can also lead to various undesirable outcomes if mistakes are made in either reference counting or memory management. Failure to properly perform either task can lead to increased development time, unwanted memory leaks, or application crashes.
Smart pointers were created to reduce the frequency of memory management errors related to the use of pointers. A smart pointer functions like a pointer except that it also automatically deallocates the memory associated with an object at the appropriate time, i.e. when the object is no longer needed. Smart pointers can also be used to maintain reference counting of dynamically allocated objects shared by multiple owners. This can be beneficial in multi-threaded applications in which several applications are being executed simultaneously. However, it can be cumbersome and time-consuming in the development stage because reference counting is still done manually through calling functions such as AddReference and RemoveReference in the programmer's code. And memory management errors still exist related to the use of smart pointers. There is a clear need for a less cumbersome and more reliable technique than the use of smart pointers especially when multi-threaded applications are involved.
Accordingly, the present invention is a novel method for providing automated memory management of server-based objects through the use of a reusable class library. The problems of accurately maintaining object pointer values are solved by using a class library to create a proxy object, which encapsulates the object along with smart pointer functionality and automated reference counting functionality. This allows reference counting to be maintained automatically as the proxy object is passed from one thread to another during simultaneous execution of multiple threads. The memory allocated to the object is automatically made available for other uses once the object is no longer needed.
In accordance with the present invention, a method of automating the memory management of server-based objects is disclosed. Initially, a new class is derived from a generic base class. The new class inherits smart pointer functionality from its base class. This is followed by the creation of a proxy object, which essentially contains the new (derived) class encapsulated in a memory management template. To achieve this, a generic template class is type-defined to the new (derived) class which “gives” the new class automated reference counting functionality. The generic template class may be part of the class library. From this point on, the reference counting on the object, i.e. the new (derived) class, is maintained automatically. The coding is significantly simplified from the use of smart pointers alone, and the object's memory location is accurately managed especially during multi-threaded applications.
These and other features and advantages of the present invention will be better understood by reading the following detailed description, taken together with the drawings wherein:
The invention provides a method for automatically managing memory allocations of server-based objects in multithreaded applications. Preferred embodiments of the invention may utilize a reusable class library to create a proxy object. This proxy object essentially includes a new class along with the functionality of smart pointers and the functionality of automated reference counting. These combined functionalities allow the new class to accurately and automatically “destroy” the memory allocated to the said class once it is no longer needed.
Referring to
Referring to
Referring to
Referring to
The exemplary embodiments described herein can be implemented in digital electronic circuitry, or in computer hardware, firmware, software, or in combinations of them. The exemplary embodiments described herein can be implemented as a computer program product, i.e., a computer program tangibly embodied in an information carrier, e.g., in a machine-readable storage device or in a propagated signal, for execution by, or to control the operation of, communications processing apparatus, e.g., a processing device, a computer, or multiple computers. A computer program can be written in a variety of programming languages, including compiled, assembled, or interpreted languages, and it can be deployed in any form, including as a stand-alone program or as a module, component, subroutine, or other unit suitable for use in a computing environment. A computer program can be deployed to be executed on one computer or on multiple computers at one site or distributed across multiple sites and interconnected by a communication network.
The present invention is not intended to be limited to a device or method which must satisfy one or more of any stated or implied object or feature of the invention and is not limited to the exemplary or primary embodiments described herein. Modifications and substitutions by one of ordinary skill in the art are considered to be within the scope of the present invention.