I trying to i populate a base class with derived classes. I was able to populate the object, but when I try to access it, my ESP restarted. This line is triggered a reset:
Impossible to answer without seeing your entire code, but it seems like you are saving a pointer to an object on the stack, and then use it after the original object has gone out of scope and has been destructed.
Either use static objects, or create them dynamically using an array of std::unique_ptr<GPIO_NONE>.
Unfortunately i need to create the object's dynamically so i need to go with the second option.
I trying to figure out how this std::unique_ptr work, i never use it before.
I think:
need to include the following 2 library:
** #include **
** #include **
create the smart pointer array
** std::unique_ptr<GPIO_NONE[]> IOs;**
add the necessary child objects to the array IOs[i ]= * new GPIO_NONE(); IOs[i+1]= * new GPIO_IN();
invoke function's IOs_.setPin(PINS*,i);[/b]* Unfortunately now freezing during the initialization :(. so i didn't get really how this smart pointer array will work_
gfvalvo:
Not sure why the special template is necessary.
RAII
It prevents memory leaks. Raw owning pointers are heavily discouraged in modern C++.
If your objects are going to live forever, you can get away with using raw pointers.
If you are going to be replacing the elements in your array, use smart pointers.
On a more serious note, it's a great solution to all kinds of problems. Because C++ always calls the constructor when an object goes out of scope (even when an exception occurs), you can be sure that all of your resources are cleaned up. This includes heap memory, but also files that must be closed, for example.
Python has a similar thing, using the with statement, and other languages like Java need special finally blocks to clean up resources, but I think the C++ approach is much easier.