Reference Manual
Inti Logo
Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | File Members

Inti::MemoryHandler Class Reference

Memory tracking class. More...

#include <inti/memoryhandler.h>

Inheritance diagram for Inti::MemoryHandler:

Inti::G::Object Inti::Atk::Hyperlink Inti::Atk::Object Inti::Atk::ObjectFactory Inti::Atk::Registry Inti::Atk::Relation Inti::Atk::RelationSet Inti::Atk::StateSet Inti::Gdk::Colormap Inti::Gdk::Device Inti::Gdk::Display Inti::Gdk::DisplayManager Inti::Gdk::DragContext Inti::Gdk::Drawable Inti::Gdk::GC Inti::Gdk::Image Inti::Gdk::Keymap Inti::Gdk::Pixbuf Inti::Gdk::PixbufAnimation Inti::Gdk::PixbufAnimationIter Inti::Gdk::PixbufLoader Inti::Gdk::Screen Inti::Gdk::Visual Inti::Gtk::AccelGroup Inti::Gtk::Clipboard Inti::Gtk::IconFactory Inti::Gtk::ListStore Inti::Gtk::Object Inti::Gtk::RcStyle Inti::Gtk::Settings Inti::Gtk::SizeGroup Inti::Gtk::Style Inti::Gtk::TextBuffer Inti::Gtk::TextChildAnchor Inti::Gtk::TextMark Inti::Gtk::TextTag Inti::Gtk::TextTagTable Inti::Gtk::TreeModelSort Inti::Gtk::TreeSelection Inti::Gtk::TreeStore Inti::Gtk::WindowGroup Inti::Pango::Context Inti::Pango::Font Inti::Pango::FontFace Inti::Pango::FontFamily Inti::Pango::FontMap Inti::Pango::Fontset Inti::Pango::Layout List of all members.

Public Member Functions

Static Public Member Functions

Protected Member Functions


Detailed Description

Memory tracking class.

The purpose of MemoryHandler is two-fold. It allows Inti objects to be created either on the heap or on the stack, and it prevents memory leaks.

MemoryHandler is based on "Item 27" from Scott Meyers book: More Effective C++. It is a memory tracking class that keeps track of objects allocated on the heap. MemoryHandler provides it's own version of operator new and delete. All "new" allocations are added to an internal allocation list. Additions are made to the front of the list. When GTK+ destroys an object, Inti calls "delete" on the C++ wrapper. delete searches the allocation list from the front, and if the object is found removes it and calls global "delete". Because the search starts at the front, and the most recently added objects are at the front, the search overhead is greatly minimized.

When a program ends MemoryHandler walks through the allocation list and calls operator delete to free any pointers it finds. This frees the raw memory a pointer points to but note - it doesn't call the object destructor. Ideally, if a program manages heap memory correctly the list should be empty, but it wont be. The list will contain a C++ pointers to any wrapped global GTK+ objects used in your program, such as Gdk::Display, Gdk::Keymap, Gdk::Screen, Gtk::Clipboard, the default Gdk::Colormap and the default Gtk::Style.

Why? If you remember, Inti adds a hook to GTK+ that calls a global "destroy_notify" function when each GTK+ object is destroyed. This function checks to see if the C++ wrapper for the GTK+ object being destroyed was dynamically allocated, and if it was calls delete (otherwise the object was allocated on the stack and nothing happens). This is how Inti manages memory. It lets the GTK+ object decide when it's time to destroy the C++ wrapper, which only happens when an object's reference count drops to zero. This causes a problem for those few default and global objects owned by GTK+. They don't get destroyed until GTK+ is removed from memory, which is never if you use the GNOME desktop. When a program ends the C++ wrappers for these objects will not be deleted and over time could cause a significant memory leak. Since these objects are few, the easiest solution was to have MemoryHandler clean up after itself.

Note: Don't use MemoryHandler as a garbage collector because it's not. It only cleans up after a program ends, not while it's running. The programmer is still responsible for allocating and freeing heap objects correctly. MemoryHandler is just a safe guard to prevent an inadvertent memory leak.


Member Function Documentation

void Inti::MemoryHandler::operator delete void *  ptr  )  [static]
 

Class-specific operator delete; inherited by derived classes.

Only deletes ptr if it's in the allocation list. You should not call delete yourself! Object deletion is handled internally by Inti.

void* Inti::MemoryHandler::operator new size_t  size  )  [static]
 

Class-specific operator new; inherited by derived classes.

Stores a pointer to each new allocation in the allocation_list. If the allocation fails the out-of-memory handler is called.


The documentation for this class was generated from the following file: Main Page - Footer


Generated on Sun Sep 14 20:08:06 2003 for Inti by doxygen 1.3.2 written by Dimitri van Heesch, © 1997-2002