0

I just create list of Coordi object as below inside of a functions. Coordi is class object which I defined.

list<Coordi> LstOfCoordi;
for (int i = 0; i < 10; ++i)
{
    Coordi *tmp = new Coordi();
    LstOfCoordi.push_back(tmp);
} 

I want release the list and each objects to the system before program termination.

If each element of list should have to be released, how to handle it ? I tried to below codes to use delete each of them, but it made compile error.

for (int i = 0; i < 10; ++i)
{
    myClass tmp = LstOfMyCalss.back();
    delete &tmp;
    LstOfMyClass.pop_back();
} 

So, I am wondering deletion from the list is enough or not. At a glance with below codes, each element was allocated and they are not released properly when the list destroyed.

for (int i = 0; i < 10; ++i)
{
    //myClass tmp = LstOfMyCalss.back();
    //delete &tmp;
    LstOfMyClass.pop_back();
} 

Please let me know the best way to release all allocated objects.

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
JJ Kim
  • 23
  • 3
  • 2
    1. Don't use C-style pointers, then you don't have to worry about such things. 2. `myClass tmp =...; delete &tmp;` is UB, always. 3. If you really want help with your code, then please provide a [MCVE]. For example, we have no idea what `list` is. (Is it `std::list`? Is it your own implementation? Is it some other third party class you are using?) 4. If you get a compile error, then please share the error with us. – Max Vollmer Jul 09 '19 at 00:27
  • If this is `std::list`, your list doesn't store pointers, it stores raw values, and shouldn't be using `new` at all. Your compiler should be rejecting this code (because you're trying to store `Coordi*` in a `list` that holds `Coordi`). The best solution is usually not to use explicit dynamic allocation at all and just store by value (in which case no special clean up is needed; when the list is destroyed, its contents are destroyed automatically for you). – ShadowRanger Jul 09 '19 at 00:48
  • Possible duplicate of [How to avoid memory leaks when using a vector of pointers to dynamically allocated objects in C++?](https://stackoverflow.com/questions/1361139/how-to-avoid-memory-leaks-when-using-a-vector-of-pointers-to-dynamically-allocat) – neuront Jul 09 '19 at 01:33
  • I know that with modern C++ we have the available libraries at our disposal for using smart pointers within the `` header, however, it still is bothersome to me that people state: "Don't use raw or C-Style pointers". I make this statement for when I first learned C++ there were no smart pointers, everything was done manually and you had to understand memory, heap allocations, pointer arithmetic, dangling pointers and references, memory management, structure alignment, deleting of pointers versus deleting pointers of arrays, cache and page alignments etc. ... – Francis Cugler Jul 09 '19 at 01:39
  • @Max Vollmer, Thanks for advice. It's first time to ask. I'll keep in mind what you said on further questions. – JJ Kim Jul 09 '19 at 01:40
  • ... you had to understand the hardware and how to communicate with it in order to tell it what to do. These are the things that separates a "coder - programmer" from a "software - engineer". I'm self taught and I learned how to properly handle memory usage! – Francis Cugler Jul 09 '19 at 01:41
  • Now with all of that being said; yes use smart pointers when and where you can as it will save a lot of time and many headaches, but there may be times that you can not use them, and then everything I have stated above will come into play. – Francis Cugler Jul 09 '19 at 01:42

1 Answers1

2

Edit: Sample code on creating list of objects using std::list.

class MyClass {
public:
    std::list<Coordi> m_listCoordi;
}

//---creation of list of objects
for(int a = 0; a < numObjects; a++){
    m_listCoordi.push_back(Coordi());  //note: no 'new' is used.
}

//---destroying list of objects
m_listCoordi.clear();

Sample code on creating list of pointer to objects using std::list.

class MyClass {
public:
    std::list<Coordi*> m_listCoordi;  //<---note: now list of pointer
}

//---creation of list of objects
for(int a = 0; a < numObjects; a++){
    m_listCoordi.push_back(new Coordi());
}

//---destroying list of objects
for(int a = 0; a < numObjects; a++){
    delete m_listCoordi[a];  //note: an item in m_listCoordi is already a pointer.
}
m_listCoordi.clear();

I apologized for a messy answer. I don't have much time to make it clean and more formal. I hope it helps anyway.


Prev answer:

If your list, say std::vector<YourClass*>, the elements hold pointer values. It's your responsibility to allocate and deallocate memory block from that pointer values that you save in the list. Whatever your method of allocating/deallocating, it's up to you. Just make sure your code won't produce memory leaks.

//---deallocate all elements before clearing the list.
for(auto& pItem : LstOfMyClass){  
    //note: since LstOfMyClass is a list of pointer values, pItem is a pointer variable.
    delete pItem;
}
//---clear the list.
LstOfMyClass.clear();

If objects in the LstOfMyClass does not move around or the list does not grow or shrink, better use list of objects instead of list of pointer to objects. With this you don't have to worry about allocating and deallocating. The list will do it for you if the list variable is destroyed.

acegs
  • 2,621
  • 1
  • 22
  • 31
  • 1
    The OP's list is probably `std::list`, not `std::vector`, and the elements it holds *aren't* pointers. This answers a question, but the OP seems pretty confused about what they're doing in the first place; I'd stick to comments to figure out their intent. – ShadowRanger Jul 09 '19 at 00:47
  • I was going to answer in a similar manner as the OP was trying to delete objects in some container that were not pointers, but you beat me to it! – Francis Cugler Jul 09 '19 at 01:35
  • Thanks that's what I want to know. I looking for examples of custom objects, but most of example uses std::list. Thanks for great help. – JJ Kim Jul 09 '19 at 01:45