Either way, here is my current solution to smart pointers.
I know this isn't extremely well commented, but hopefully you get the idea. Besides, it's almost 2:00 A.M. and I'm about to pass out.
I will note, however, that the main reason for this design is to make the storage maps smaller by keeping them grouped by object type through templates. This makes traversing them a bit faster when needing to add a new reference. Also be aware that the overhead of std maps is not light. I intend on implementing hash tables/sets, lists, and other data structures later on to help with this a bit.
I also wrote a handy little console test app to log all of my pointer assignments and check for memory leaks:
// Check for memory leaks at the end of the application.
// Results will show in the Output window.
_CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
Logger = new CLogger("ErrorLog.html");
Logger->Write(COMMENT, "#0000BB\">Beginning First Test");
Logger->Write(COMMENT, "Allocating and Creating iTest1");
mem_ptr<int> iTest1 = new int;
Logger->Write(COMMENT, "Creating iTest1_Copy");
Logger->Write(COMMENT, "Copying iTest1 to iTest1_Copy");
iTest1_Copy = iTest1;
Logger->Write(COMMENT, "#0000BB\">Ending First Test");
Logger->Write(COMMENT, "#0000BB\">Beginning Second Test");
Logger->Write(COMMENT, "Creating iTest2");
Logger->Write(COMMENT, "Allocating iTest2");
iTest2 = new int;
Logger->Write(COMMENT, "Creating and Allocating iTest2_Copy");
mem_ptr<int> iTest2_Copy = new int;
Logger->Write(COMMENT, "Assigning iTest2_Copy to iTest2.
This should dereference iTest2_Copy's initialized allocation and reassign it");
iTest2_Copy = iTest2;
Logger->Write(COMMENT, "#0000BB\">Ending Second Test");
// Intentionally neglect to release 2 of the objects
// to verify the app is still killing them. This
// will generate warnings.
Logger->Write(COMMENT, "#0000BB\">EXITING APPLICATION");
/* Return error free */
Here is what this log looks like:
The reason for the warnings at the bottom is that I am testing to make sure that the objects get destroyed regardless of the Garbage Collector getting ahold of them. If the last reference to the object goes out of scope before it is released and inserted into the Garbage collector, then the smart pointer is forced to delete the object during destruction. This is not preferred and is why you see a warning when I fail to release iTest1_Copy and iTest2. Also note that the smart pointers are cleaning up for me even once I return from main(); This is noted in the log after "EXITING APPLICATION".
The reason the logger continues to work is because I made it static for testing purposes. It will be a singleton when implemented and will no longer be created as a mem_ptr.
As you can see, I haven't gotten around to implementing the XML/JS version of the logger yet. This is just a place holder for the mean time.
Feel free to let me know what you think; Love it, hate it, whatever.
Thanks again for looking!