Managing memory in objects in a C ++ collection

I have a map that associates integers with vectors (of objects). These vectors are a set of tasks to perform. To reduce the amount of copy when using this map and vector, I set them to use pointers.

std::map<int, std::vector<MyObject *> *> myMap;

During the initialization of the class containing myMap, I populate myMap, creating a new vector populated with new MyObjects.

For me, though, it's memory management. Now I have these various objects sitting somewhere on the heap, and I am responsible for cleaning them when I am done with them. I also know that I will NEVER work with them until the program is completed. But after about 10 weeks, when someone decides that a smart way to change this application involves removing elements from the map / vectors. This will result in a memory leak.

My question is, how can I handle the proper release of these objects so that even if they were deleted using the STL function, what objects were deleted successfully?

Your help is much appreciated, let me know if I missed anything critical! Thanks!

+3
source share
6 answers

I agree that using smart pointers is a good way, but there are at least two alternatives:

a) Copying may not be as expensive as you think. Try to implement a value map

std::map<int, std::vector<MyObject>> myMap;

b) Replace the vector with your own class that wraps the vector. In this class destructor, handle the release. You can also provide methods for adding and removing MyObjects.

+4
source

Use the smart boost: shared_ptr pointer, not raw pointers, so when the object is destroyed, it will also clear the allocated heap of memory.

boost :: shared_ptr http://www.boost.org/doc/libs/1_39_0/libs/smart_ptr/shared_ptr.htm

? , std:: map ( , / , , , ).

EDIT: shared_ptr tr1, , , . , STL, , . Google .

EDIT2: Visual Studio 2008 TR1 shared_ptr, Visual ++ 2008 Feature Pack. , , , , TR1, , VS, TR1.

+7

( ) - .

, , . , , - 10 .

+1

. , . , std:: auto_ptr - , . , , .

- . RAII, , , , "".

, . , , , .

RAII
++
Boost Smart Pointers
/

+1

/, , , " " .

0

, map<,vector<Object>>:

Take a close look at the mapand interfaces vector. They basically return links to the elements they contain, and if you keep the link when transferring these things, copying will not happen.

Bad example:

std::vector<MyObject> find_objects( const std::map<int,std::vector<MyObject>> & map, int i ) {
    const std::map<int,std::vector<MyObject>>::const_iterator it = map.find( i );
    if ( it != map.end() )
        return it->second;
    else
        return std::vector<MyObject>();
}
// ...
const std::vector<MyObject> objects = find_objects(/*...*/);

better:

const std::vector<MyObject> & find_objects( const std::map<int,std::vector<MyObject>> & map, int i ) {
    const std::map<int,std::vector<MyObject>>::const_iterator it = map.find( i );
    if ( it != map.end() )
        return it->second;
    static const std::vector<MyObject> none();
    return none;
}
// ...
const std::vector<MyObject> & objects = find_objects(/*...*/);

-> no copy

0
source

Source: https://habr.com/ru/post/1713044/


All Articles