Introduction
Pointers always envied automatic cleanup that is in c++ provided only for big fat slow Static Object or their arrays stored by value. In this article we will introduce concept of scopes also for dynamic objects and scoped version of new/malloc to take advantage of those scopes. Ie associated objects/memory will get destroyed with the scope. Plus your code will now be smaller and simpler. I got this idea about pointers deserving automatic cleanup love too in my other kinda lame article about
new && operator
struct Object : Scope::Obj {
};
Scope global;
void proc() {
Scope local;
char* text = strdup(local,"text"):
Object* obj = new(global) Object();
Object* array[1000]={0}; for(int i=0;i<1000;i++) {
array[i] = new(local) Object(); }
}
Why manual cleanup is source of bugs.
| Automatic cleanup provided for static objects saves time lines of code and a lot and I mean really lot of bugs. Bugs by forgetting to match all allocations with deallocations.
Bugs by memory being leaked when exception or error handler exits something somewhere prematurely and not all control paths contain correct number of release statements. |
Bugs by releasing objects twice or more and corrupting memory by confused conditional statements or bad multithreaded code.
Performance point of view
So we definitely want automatic cleanup that is provided for static objects. But pretty much any program serious about performance will store objects by pointers and not by value because price for reallocation insert or sort is horrendous as you can see in benchmark
here.
So can't we have both? Performance and efficiency of pointers and safety and simplicity of static objects by automatic cleanup when leaving scope? Yes we can if we implement concept of scopes also for dynamic objects.
Simpler alternative to smart pointers
Not that I think that STL/Boost are not important and usefull. They are in zilion cases since it doesn't make sense to reinvent things again and again.
Litle advantage of scope.h is that you don't need kinda heavy dependency on stl/boost just for simple thing like pointer. Code simplicity and clarity keeps bugs at bay. Why ?
In sample code bellow is clear what is going on with basic pointer types not limiting you to any storage imaginable.
ptr1 = ptr2; *ptr1 = *ptr2;
With smart pointers the fact when they actually copy or move is one big jungle and most of them even don't support storage to arrays
Most of them simply don't work in containers. some require c++11 compiller or big template libs resulting to big executables and all make code unusable by others since libs/dlls with stl/boost are incompatible on binary level not having any concept of stable interface .
auto_ptr is very bad choice. It's pointer with copy and with move semantics and ownership (=auto-delete) can't be stored in containers
scoped_ptr is a auto_ptr without copy and without move semanticscan be stored in containers.and requires boost It adds a level of safety. memory created will exist only for that scope and no more, thus you get compile-time protection against attempting to move it or transfer it.
unique_ptr is a auto_ptr without copy but with move semantics.can be stored in containers requires c++ 11 compiller. Ownership is movable,
shared_ptr. require c++ 11 compiller and are reference counted ie. cleanup when all referencers are gone,
weak_ptr ,intrusive_ptr ,... whatever new ones will come.
With smart pointers you need to wrap every pointer reference ie change all the code to alien lenghty syntax always worying about state machine you just created. Take for example this guy having trouble sorting simple array of pointers which he normally wouldnt have problem to create but now obviously having noidea what state machine he actually created he is sitting in forums getting responses like
"...The IS specifies that a predicate should not assume that the dereferenced iterator yields a non-const object. Arguments should therefore be taken by reference-to-const or by value. The IS also specifies that
algorithms that take function objects as arguments are permitted to copy those function objects. So in most cases, the overloaded function call operator would be a const member function ..." | Hmm.. |
It is unbelivable what complexity we vere able introduce to ehm... "simplify our life"
The real solution is to have smart pointers build in c++ language and not create zillion of weird templates supporting this and not that. For example let Object *~ be declaration of smart pointer. Will have destructor called going out of scope and all of the copy move is already part of low level pointer arithmetics. no zillion of template wrapers/libs/operators. period. I am right now attempting to implement it in fantastic CLang /gcc compillers as an interesting experiment. Visual c++ is closed source so Thank's god for whole open source movement.
But until then with scopes you just derive your objects from Scope::Obj in one line and that's all. Keeping simple easy to debug code and libs/dlls usable by others. Just insert scope to your class and use scoped de/allocators. All your data will be properly deallocated exactly like with smart pointers even when constructor fails in the middle during exception etc.
struct A {
Scope local;
B* ptr1;
B* ptr2;
A() {
ptr1=new(local) B();
ptr2=new(local) B();
}
}
So if you are like me preferring things small clear simple and fast. If you don't like obfuscation for simple things and like to know by fast look at source what is going on.
Let's try simpler but I hope equally functional alternative.
Let's Scopes for dynamic heap objects be born
| So how does the automatic cleanup of static objects actually work?
Well. It works by simply adding pointer of every static object within scope to internal invisible linked list and when leaving calling destructors for each of them in reverse order. |
Can we make something similar for dynamic objects?
Why not. We can have array of void pointers and store pointers to various object types in one single array.
The only issue are calling proper object specific destructors. Unfortunately as far as I know c++ doesnt allow to get destructor adresses. So the only sollution I come up so far is to use virtual destructor in all stored objects so polymorphysm selects proper one for us.
Implementation of Scope.h
This code snippet bellow is kept simple with compressed formating just for purpose of article so it is clear what is going on. Complete implementation along with scoped versions of new delete strdup malloc calloc free and threadsafe version of scope called TScope (usefull for using for example global scope from multiple threads etc) is in Scope.h that is attached in zip file with Example.cpp on top of the article. I did not attached project files since most of you will not be able to open vs2012 anyway.
struct Scope { struct Obj { virtual ~Obj() {} };
Scope* prev; Obj* ptr;
Scope() : prev(0) , ptr(0) {}
~Scope() { ptr->~Obj(); free(ptr); delete prev; } };
inline void * __cdecl operator new(unsigned int size,Scope& scope)
{
void *ptr = (void *)malloc(size); if(scope.ptr) { Scope* prev=0; prev=(Scope*)calloc(sizeof(Scope),1); *prev=scope;
scope.prev=prev; }
scope.ptr=(Scope::Obj*)ptr;
return(ptr);
};
|
Limitations of this solution
In C language Scope.h usage is very simple and straightforward. You asociate pointers with scope and they are freed automatically. In C++ thou you need to call destructors first. Unfortunatelly C++ doesn't support getting address of destructor. So as slight inconvenience. To support automatic cleanup your objects need to be derived from Scope::Obj or to have virtual destructor as first virtual method. That being said adding one derive statement in class declaration is one changed line of code ie less work and changes than going about replacing every ocurence of pointer of that class with long alien syntax as you need with smart-pointers .
Exception Handling Since we want Scope to cleanup everything for us no matter what exception happens we must keep scope declaration outside try catch statement (not within). Otherwise scope can't do what it is supposed to do (cleanup for all possible exit points).
proc() {
Scope local;
try {
Object* array[1000]={0}; for(int i=0;i<1000;i++) {
array[i] = new(local) Object(); }
} catch(...) {
printf("no manual cleanup needed anymore");
}
}
|
In case of exception as part of exception unwinding C++ calls our scoped version of delete which deregisters failed object from scope so when scope is destoyed this object is gone (no free or destructor called)
Example code
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include "Scope.h"
Scope global;
struct A : Scope::Obj {
A(){ printf("\n A %x",this); }
~A(){ printf("\n ~A %x",this); }
};
void Test() {
Scope local;
A* array[3]={0};
for(int i=0;i<3;i++) {
array[i]=new(local) A();
}
char* text=strdup(global,"this will get deallocated on program exit");
A* a=new(global) A();
}
void main() {
Test();
}
Output:
A 689718 A 68b110 A 68b198 A 68b220 ~A 68b198 ~A 68b110 ~A 689718 ~A 68b220
Points of Interest
The fact that derive from Scope::Obj or with virtual destructor as first virtual method kinda is slight inconvenience considering what you will gain.
Also if you want to autodeallocate memory from malloc/calloc/strdup just use scoped versions from scope.h in zip file. Off coarse for explicit dealocation you must use scoped versions of free/delete too so they are removed from scope and no another attempt is made to free them when leaving scope
Building automatic cleanup to c/c++ language via experimental switch
I am trying to implement smart pointers (special pointer that has destructor/free called when leaving scope or containing object is destroyed) as build in to c and c++. so low level os c/c++ developers can produce smaller and safer code too without big template libs .
GCC: -fsmart-pointers switch implementation status.
So far I am just studying how is static object cleanup actually implemented.
In first phase gcc transforms source to so called gimple form
struct A{ int a;
~A(){}
};
int main(int argc,char**argv) {
A a[12];
return 0;
}
gcc -O0 --dump-tree-gimple test.cpp produces form in wich generated code for automatic cleanup of static objects (Loop generating this pointers and calling destructors) is clearly visible
try
{
D.1868 = 0;
return D.1868;
}
finally
{
{
struct A * D.1866;
D.1866 = &MEM[(void *)&a + 48B];
<D.1869>:
if (D.1866 == &a) goto <D.1870>; else goto <D.1871>;
<D.1871>:
D.1866 = D.1866 + -4;
A::~A (D.1866);
goto <D.1869>;
<D.1870>:
}
}
So far it seems that this code is generated gcc\cp\init.c in proc build_vec_init
I am open to interesting new ideas or suggestions . But help with gcc switch implementation is welcomed most.