Author Topic: Smart Pointers  (Read 3888 times)

0 Members and 1 Guest are viewing this topic.

Chuck Gabriel

  • Guest
Smart Pointers
« on: December 10, 2005, 10:18:13 PM »
I have been asked to give an explanation of what smart pointers are and why you might want to use them in your ObjectARX programs.  Here goes.

To understand why there is a need for such a thing as a smart pointer, it is important to first understand some basic facts about memory management in C++.  When you allocate memory for a variable in C++, you can take that memory from either the stack or the heap.  Variables placed on the stack are sometimes called automatic variables, because when they go out of scope (I sure hope everyone knows what that means) the memory they used is returned to the stack automatically.  Variables placed on the heap are called dynamic variables, because the size and location of the memory they use can change over their lifetimes.  Dynamically allocated variables have to be told explicitly to release the memory allocated to them.  Dynamically allocated variables will always be pointers.  If you don't know what a pointer is, that is a topic for another discussion altogether.

Consider this block of code:

Code: [Select]
void test() {
  int x = 0; // automatic variable
  int* pY = new int(1); // dynamic variable
  delete pY; // pY deallocated here
} // all variables in the preceeding block go out of scope here.

The memory for x gets release at the closing brace of the function.  However, if I had neglected to release the memory for pY (via delete), I would have a memory leak, and since pY had gone out of scope I would have no way of correcting my mistake.

This is where smart pointers come in.  Smart pointers wrap a dynamically allocated variable in a class that manages the allocation and deallocation of memory automatically.  Here is an example:

Code: [Select]
class AcDbDatabasePointer {
  AcDbDatabase* pDb; // The wrapped pointer
public:
  AcDbDatabasePointer(bool buildDefaultDrawing = true,
                                bool noDocument = false) :
  pDb(new AcDbDatabase(buildDefaultDrawing, noDocument)) {} // memory gets allocated in the constructor
  ~AcDbDatabasePointer() {
    if(pDb) delete pDb; // and deallocated in the destructor
  }
  AcDbDatabase* operator->() {
    return pDb; // forward member function and variable calls to the wrapped object
  }
  AcDbDatabase* object() {
    return pDb;
  }
  void close() {
    if(pDb) delete pDb;
    pDb = 0;
  }
};

Here is how you would use that in application code:

Code: [Select]
void test() {
  AcDbDatabasePointer db;
  // do something with the database.  maybe draw a nice smiley
}  // db goes out of scope here.  It's destructor gets called automatically, which takes care of deallocating the memory used by the wrapped pointer.

Clear as mud?

The ObjectARX API provides several different types of smart pointers right out of the box.  Probably the most commonly used smart pointer in ObjectARX would be the AcDbObjectPointer template class.  Smart pointer classes frequently are template classes for reasons that will become apparent once you begin to explore them for yourself.  There are also smart pointer classes for symbol tables, symbol table records and probably some other things I'm not aware of.

The choice to use them or not is yours, but I think you will find that they dramatically decrease the opportunity for memory management errors in your code, and they frequently make your code more compact and concise.
« Last Edit: December 10, 2005, 10:21:21 PM by Chuck Gabriel »

LE

  • Guest
Re: Smart Pointers
« Reply #1 on: December 11, 2005, 01:54:27 PM »
Perfect.
« Last Edit: December 12, 2005, 09:27:07 AM by LE »

Swift

  • Swamp Rat
  • Posts: 595
Re: Smart Pointers
« Reply #2 on: December 11, 2005, 03:37:42 PM »
Nice post Chuck!

Draftek

  • Water Moccasin
  • Posts: 1501
Re: Smart Pointers
« Reply #3 on: December 12, 2005, 06:36:48 AM »
Thanks a bunch, Chuck.
Soli Deo Gloria

Chuck Gabriel

  • Guest
Re: Smart Pointers
« Reply #4 on: December 12, 2005, 07:39:57 AM »
Thanks for the feedback guys.  It's nice to know I'm not the only one interested in this stuff.

MickD

  • Gator
  • Posts: 3290
  • (x-in)->[process]->(y-out)
Re: Smart Pointers
« Reply #5 on: December 13, 2005, 12:06:54 AM »
Very good and very clear, thanks Chuck.
Debugging:
Being the detective in a crime movie where you're also the murderer.

“Someone's sitting in the shade today because someone planted a tree a long time ago.”
- Warren Buffet

LE

  • Guest
Re: Smart Pointers
« Reply #6 on: December 13, 2005, 02:56:24 PM »
If one of these days you have a chance and would like to expose something about MFC dialogs/using the ARX SDK or know about the dockbar control....

 :-)

Chuck Gabriel

  • Guest
Re: Smart Pointers
« Reply #7 on: December 14, 2005, 07:32:16 AM »
If one of these days you have a chance and would like to expose something about MFC dialogs/using the ARX SDK or know about the dockbar control....

 :-)

The truth is I know very little about MFC.  I've done a couple of very simple dialogs using only the most basic controls.