Replacing existing source pointers with smart pointers

Note. This may seem silly. I have an application that uses raw pointers, and there are a lot of memory leaks in the application.

My question now is how easy it would be to replace existing raw pointers with smart pointers. And simply replacing them will help reduce memory leaks caused by not freeing dynamically allocated memory.

To explain a little further, this application is deprecated, and there are very simple memory leaks where memory will be allocated and not released in the same function.

I did a memory analysis using DevPartner and found many areas. Is Valgrind Better Than Devpartner.

+1
c ++ boost
source share
5 answers

Using smart pointers will certainly be a good start to clean up your application, but they cannot be cured. Many memory leaks may just be carelessness in a well-designed program, but you most likely have serious design issues, and a memory leak is a symptom of this. When you switch to smart pointers, you still have to make design options such as “Who owns this object”, “Is ownership of this object divided between several clients”, and “What is the expected lifetime of this object” to choose the right one implementation of a smart pointer for this scenario. But this will be a good way to start, because the process of choosing the right taste for a smart pointer for different situations will make you think about these things and will probably improve your design.

+3
source share

There is no doubt that smart pointers reduce the load on wrt programmer's memory problems. Since you did not mention whether this is an outdated application or how easy it is to change interfaces (not always required, since smart pointers degrade to raw pointers), I would suggest running application under some tool, for example valgrind (on Linux) or clean up (on Unices and Windows) to track a memory leak.

In my experience, most memory leaks follow a certain pattern (developer A skips something, developer B copies this code and, at the same time, the problem). Thus, using tools, you can solve memory leak problems before thinking about using smart pointers.

And if you are starting to develop this application, start by using smart pointers. They will save you a lot of time and energy on the line.

+2
source share

I am going to give it firmly "very possible."

One form of memory leak is when a function allocates memory and does not remove it from at least one path from the function. Some area pointer, such as auto_ptr , can handle this very well. (Note: auto_ptr was part of the first ISO standard, and it is deprecated in the next, so it’s hard to give recommendations on which pointer to use for the scope. Consult your compiler and library documentation to find out what they support.)

Another form is where the object is allocated and the property is divided, which means that there is more than one procedure that uses it, and there is no easy way to tell if everything is connected with it. This is a good candidate for shared_ptr , but you should use some caution. When creating an object, assign shared_ptr and make sure that every other pointer uses shared_ptr . If routines A, B, and C access the object through shared_ptr , and you have not changed D, then when routines A, B, and C pass through it, it leaves, regardless of the needs of D.

One thing you want to avoid with shared_ptr is loops, because if I have shared_ptr to J, J has a shared_ptr to K, and K has a shared_ptr to I, none of them are ever deleted , even if they are not available anywhere in the program. You need to monitor these situations. You can break them into weak_ptr in a loop or delete an element in a loop yourself or just live with a leak.

Another thing to consider is replacing vector and similar containers for dynamically allocated arrays and other dynamically allocated data structures. This gives you plenty of free time to manage your memory, although you can still flow from memory anyway. If the vector becomes large and then becomes small, it will not free up excess memory. A common practice, if you really need to recover memory, is to swap once a large vector with a copy of itself.

In short, there are many very useful tools for automating memory management, but they do not replace thinking on your part. Many problems can be solved by simply turning each pointer into shared_ptr and each array into vector , but using it wisely will give even greater benefits.

+2
source share

It depends on the fact that the garbage collector may still have memory leaks if hard links to objects do not allow the garbage collector to free it. If you want to find out where a memory leak occurs, use the profiler or write unit tests and an object layout.

Edit: To get the benefits of smart pointers, you also need to hook up or implement your own garbage collector, since it is not a language feature.

Edit 2: Smart pointers seem to implement reference counting, which is a garbage collection strategy.

+1
source share

Smart pointer document in the document in which the objects are stored. Therefore, for each pointer, you must convert the "undefined document that was in some programmer chapters" to this "exact document in code." Most options are easy. This still leaves many options that are not. Also, besides ownership, you need to worry about breaking the loops of object objects with weak pointers. Thus, this general conversion to smart pointers is not a trivial task, and if you made a mistake in the part related to the loop, you will even introduce new memory leaks.

0
source share

All Articles