Click here to Skip to main content
Click here to Skip to main content
 

new(local) for safer and simpler code with no need for smart-pointer template wrappers

, 2 Oct 2012
Rate this:
Please Sign up or sign in to vote.
reduce bugs and memory leaks by using new(local)
This is an old version of the currently published article.

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 an simpler. I got this idea about pointers deserving automatic cleanup love too in my other article about new && operator

Scope global;
// entering scope
{ 
    Scope local;
    char*  text = strdup(local,"text"): // malloc based memory have automatic cleanup too
    
    Object* obj = new(global) Object(); // obj will get destructor called on program exit 

    Object* array[1000]={0};            //sorting pointers is fast compared to array of objects
    for(int i=0;i<1000;i++) {
        array[i] = new(local) Object(); // creates object associated with "local" scope 
    }
}
// we left scope so "local" and all associated object /memory 
// gets destructors called/freed in proper order    

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 condition statements.

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

Another nice advantage of scopes is that you don't need dependency on  these two 

guys
and wrap everything to kinda big smart pointer templates in your class keeping simple easy to debug code.
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, *ptr2;
    A() {
        ptr1=new(local) B();
        ptr2=new(local) B();// will not allocate and will leave prematurely due to memory 
                            // exhaustion exception. But no memory leak will happen since scope
                            // deallocates ptr1 on scope destruction as part of object
                            // destruction that follows this constructor exception
    }
}

Reinventing Wheel    

Turns out that I am not the first one thinking along those lines and Boost template library already provides us ptr_vector.  vector of shared_ptr or shared_array.
Templates are great when you are not creating library (stl/boost are binary incopatible between versions/compillers) and when you don't mind wrapping every variable to template pretty much rewriting all your code and when everything is working. Take for example this guy having trouble sorting simple array of pointers and geting 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"

So if you are like me preferring things small and simple so you know by simple look at source what is going on.
Let's try simpler but I hope equally functional alternative approach writen on 10 lines.  

Can we have Scopes for dynamic heap objects Please ?  

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 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 { // This is Just simple linked list
       struct Obj { virtual ~Obj() {} };
       Scope*    prev;  Obj* ptr;
       Scope() : prev(0) ,   ptr(0) {}	
      ~Scope() { delete ptr; delete prev; } // deleting all childs in reverse order on exit
};

inline void * __cdecl operator new(unsigned int size,Scope& scope)
{
    void *ptr = (void *)malloc(size); // we add all new objects to this linked list
    if(scope.ptr) { Scope* prev=0; prev=(Scope*)calloc(sizeof(Scope),1); *prev=scope;
    scope.prev=prev; }
    scope.ptr=(Scope::Obj*)ptr; 
    return(ptr);
};

This very simple nothrow new overload just as proof of concept;
Also Scope is very primitive linked list kept simple on purpose. 
 
Beware: only objects having virtual destructor should be associated with scope. 

Example code 

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include "Scope.h"   

Scope global; // Deallocates/Destroys all associated objects on program exit no matter 
              // what exception or error in program happens

struct A {    // As for supported objects. They must unfortunately have virtual destructor
             A(){ printf("\n  A %x",this); }
    virtual ~A(){ printf("\n ~A %x",this); }
};

void Test() {
    Scope local; // all objects associated with scope will be deallocated by leaving procedure
    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();  // this will get destructor called on progam exit
}

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 so far I managed to autodeallocate only objects with virtual destructor kinda sadens me. But I hope that some way to get around this limit is found since deriving from std::string etc just to make it' destructor virtual to allow it's autorelease is kinda unelegant. 

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 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Ladislav Nevery
Software Developer (Senior)
Slovakia Slovakia
Past Projects:
[Siemens.sk]Mobile network software: HLR-Inovation for telering.at (Corba)
Medical software: CorRea module for CT scanner
[cauldron.sk]Computer Games:XboxLive/net code for Conan, Knights of the temple II, GeneTroopers, CivilWar, Soldier of fortune II
[www.elveon.com]Computer Games:XboxLive/net code for Elveon game based on Unreal Engine 3
ESET Reasearch.
Looking for job

Comments and Discussions


Discussions posted for the Published version of this article. Posting a message here will take you to the publicly available article in order to continue your conversation in public.
 
QuestionDidn't know there is a new operator in C PinmemberStan Mihai18-Jul-13 4:41 
QuestionWhy not std::unique_ptr<T>? [modified] PinmemberC++ Pirate Programmer18-Oct-12 5:46 
AnswerRe: Why not std::unique_ptr? [modified] PinmemberLadislav Nevery18-Oct-12 16:02 
QuestionThanks PinmemberYannAchard18-Oct-12 1:29 
AnswerRe: Thanks PinmemberLadislav Nevery18-Oct-12 13:00 
GeneralMy vote of 2 PinmemberSeattleC++15-Oct-12 9:53 
GeneralRe: My vote of 2 PinmemberLadislav Nevery16-Oct-12 9:56 
GeneralRe: My vote of 2 PinmemberSeattleC++16-Oct-12 11:24 
GeneralRe: My vote of 2 [modified] PinmemberLadislav Nevery17-Oct-12 5:44 
GeneralRe: My vote of 2 PinmemberSeattleC++17-Oct-12 10:29 
GeneralRe: My vote of 2 [modified] PinmemberLadislav Nevery17-Oct-12 23:06 
QuestionWhat's about an exception handling? Pinmemberkonik1015-Oct-12 6:05 
AnswerRe: What's about an exception handling? [modified] PinmemberLadislav Nevery16-Oct-12 1:47 
GeneralRe: What's about an exception handling? [modified] Pinmemberkonik1023-Oct-12 21:24 
GeneralRe: What's about an exception handling? PinmemberLadislav Nevery24-Oct-12 22:58 
GeneralRe: What's about an exception handling? Pinmemberkonik1025-Oct-12 1:20 
GeneralRe: What's about an exception handling? PinmemberLadislav Nevery25-Oct-12 4:41 
GeneralRe: What's about an exception handling? [modified] Pinmemberkonik1025-Oct-12 5:43 
GeneralRe: What's about an exception handling? PinmemberLadislav Nevery25-Oct-12 12:50 
QuestionException / Thread safety? PinmemberPetro Protsyk8-Oct-12 1:20 
AnswerRe: Exception / Thread safety? PinmemberLadislav Nevery8-Oct-12 7:54 
GeneralMy vote of 2 PinmemberPatrick Niedzielski7-Oct-12 5:38 
GeneralRe: My vote of 2 [modified] PinmemberLadislav Nevery7-Oct-12 10:26 
GeneralRe: My vote of 2 PinmemberPatrick Niedzielski7-Oct-12 10:30 
GeneralMy vote of 5 Pinmembernv33-Oct-12 22:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 2 Oct 2012
Article Copyright 2012 by Ladislav Nevery
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid