How to release memory | Singleton | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula gr√°tis
+ 1

How to release memory | Singleton

Hi Do we need to release memory for singleton method? It is static so we need to release memory or not? If required, why destructor is not called? If not required, then why it is so as we are doing new but not delete? Also why uncommenting line related to rvalue reference is giving error? below two lines are giving compilation error: Singleton(const Singleton&&) noexcept = default; Singleton& operator= (const Singleton&&) noexcept = default; https://code.sololearn.com/cA90A10A11A0

29th Jan 2021, 10:46 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
5 Respostas
+ 1
It's good practice to delete something allocated with new, especially if the object has a destructor that is not empty. Only delete something when you will never use it again. You most likely want your singleton to live for as long as the program is running so not deleting it should be fine. I don't see how the singleton with a pointer and new/delete could be better than using a static local variable. Let the compiler manage memory for you when you don't need to. Edit: I think you should follow the singleton pattern mentioned in the accepted answer if you can: https://stackoverflow.com/questions/1008019/c-singleton-design-pattern
30th Jan 2021, 10:16 AM
jtrh
jtrh - avatar
+ 2
No, its not required or necessary as most operating systems will free the memory when the program exits. If you really want to, you could call delete on the pointer and then set it to nullptr but the way you are doing it in the destructor will cause infinite recursion because delete pSingleton will call the destructor again. Better yet if you don't want to worry about new and delete you can use a static local variable instead: // Or return a reference Singleton& so that you don't have to deal with pointers Singleton* Singleton::getInstance() { static Singleton s; return &s; } Your move constructor and assignment operator should take a non-const rvalue reference (Singleton&&). As the point of declaring them as private is so that you can't use them, you don't have to provide an implementation. Singleton(Singleton&&); is enough. Since you seem to be using C++11's "= default" you might as well use its "= delete": Singleton(Singleton&&) = delete; Singleton& operator= (Singleton&&) = delete; https://en.cppreference.com/w/cpp/language/function#Deleted_functions
29th Jan 2021, 11:53 AM
jtrh
jtrh - avatar
+ 1
Yeah , working without const for rvalue ref.... Also I don't want to delete as it is already private and no one can be used it outside of class with default also... Thanks a lot .... Got your point of static reference object also... Only issue I could not understand is delete on the pointer.... Does it means I should delete in main function from pointer I have created ? There are two different pointer pointing to same static object , so two times I should not delete it... Hence how to do this ? Also it's static and if I will delete from main at my wish , doesn't it violate static concept that static is valid through out the program scope ?
30th Jan 2021, 6:24 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 1
Yeah getting your points... Only question is isn't it bad not to delete once we use new ? I got your point of additional method for delete but it again ask for main method object point to null... Smart pointers are also good suggestion But what makes me feel bad is that is it too complicated to use singleton with pointer ? Yes , with reference of static is straight forward but what should be best if one has static pointer for singleton ?
30th Jan 2021, 9:54 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
"= delete" works without private and it describes your intention better. Yeah you can keep it the way you're doing it now and it will work, but I think "= delete" will give you a clearer error message. Note: Your move constructor/assignment operator will not be implicitly defined since you defined the destructor/copy constructor/copy assignment operator. (see "Implicitly-declared move constructor" https://en.cppreference.com/w/cpp/language/move_constructor) I just realised that your destructor is private, so you have to delete it using a static method, not in main: void Singleton::deleteInstance() { delete pSingleton; pSingleton = nullptr; } // At the bottom of main, probably Singleton::deleteInstance(); You only call this method once, after doing what you need to do with the singleton. Although this is pretty awkward as pObj1 and pObj2 will be pointers pointing to freed memory, you should set them both to nullptr as well. pSingleton is a static pointer which means it lives as long as the program does. The pointer is static, not the object pointed to. You can delete the object whenever you want as you've allocated it with new. One thing that comes to mind when dealing with multiple references to an object is std::shared_ptr and std::weak_ptr. I don't know if you should really do it in this context but here: https://code.sololearn.com/cA6A1165a9A1 I'm not sure why you still want to delete the singleton though? Maybe a singleton isn't what you need.
30th Jan 2021, 8:38 AM
jtrh
jtrh - avatar