Oooppsss...I noticed a strange behaviour during memory allocation/deallocation...
It seems that it make one more deallocation than allocation.
I added 2 new member vars in
track_allocator<T>
one is
allocCount
and the other
deallocCount
. Also I print out these 2 new vars in destructor. On each successfull allocation (that is, pointer not null from base member function
allocator<T>::allocate()
I increment allocCount and on each non-null pointer to deallocate I incremenet deallocCount.
In the copy-constructor I simply copy the values of the member vars from one instance to the other.
This is the test code:
1 2 3 4 5 6
|
typedef std::set<int, std::less<int>, track_allocator<int> >IntSet;
{
IntSet s;
for(int i=0;i<1000;i++) s.insert(i);
}
| |
The output i get is:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
|
Requested: 1 of size 8
*********** Destructor ************
Current: 8
Peak: 8
# alloc: 1
# deall: 0
************ End Destr ************
*********** Destructor ************
Current: 0
Peak: 0
# alloc: 0
# deall: 0
************ End Destr ************
Requested: 1 of size 20
*********** Destructor ************
Current: 0
Peak: 0
# alloc: 0
# deall: 0
************ End Destr ************
*********** Destructor ************
Current: 0
Peak: 0
# alloc: 0
# deall: 0
************ End Destr ************
Requested: 1 of size 20
Requested: 1 of size 20
Requested: 1 of size 20
Requested: 1 of size 20
Requested: 1 of size 20
Requested: 1 of size 20
Requested: 1 of size 20
Requested: 1 of size 20
Requested: 1 of size 20
Requested: 1 of size 20
Removing: 1 of size 20
Removing: 1 of size 20
Removing: 1 of size 20
Removing: 1 of size 20
Removing: 1 of size 20
Removing: 1 of size 20
Removing: 1 of size 20
Removing: 1 of size 20
Removing: 1 of size 20
Removing: 1 of size 20
Removing: 1 of size 20
Removing: 1 of size 8
*********** Destructor ************
Current: 4294967288
Peak: 220
# alloc: 11
# deall: 12
************ End Destr ************
*********** Destructor ************
Current: 0
Peak: 0
# alloc: 0
# deall: 0
************ End Destr ************
*********** Destructor ************
Current: 0
Peak: 220
# alloc: 11
# deall: 11
************ End Destr ************
| |
That is...destructor called 7 times! I think it does some dirty jobs with copy-ctor and so on...
Well the problem disappear when I put the counting vars outside the class (non member) and I clear the constructor and the copy constructor, so this is:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
template<typename U>
struct rebind {
typedef track_allocator<U> other;
};
track_allocator() : allocator<T>() { }
// copy-ctor
track_allocator(const track_allocator&) { }
template<typename U> track_allocator(const track_allocator<U>&) {
// rest of the class
| |
Now the counting is good...same # of allocation and deallocation and bytes are ok.
I will use this version now...but I just want to understand what happens.
If someone can explain me I would be very happy!
Thank you to everyone helped me so far!!