Click here to Skip to main content
15,886,963 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: get_string memory leak? Pin
Mircea Neacsu5-Feb-24 14:13
Mircea Neacsu5-Feb-24 14:13 
GeneralRe: get_string memory leak? Pin
mike74115-Feb-24 20:05
mike74115-Feb-24 20:05 
GeneralRe: get_string memory leak? Pin
Mircea Neacsu5-Feb-24 20:22
Mircea Neacsu5-Feb-24 20:22 
GeneralRe: get_string memory leak? Pin
Dave Kreskowiak6-Feb-24 4:39
mveDave Kreskowiak6-Feb-24 4:39 
GeneralRe: get_string memory leak? Pin
Mircea Neacsu6-Feb-24 6:59
Mircea Neacsu6-Feb-24 6:59 
GeneralRe: get_string memory leak? Pin
Dave Kreskowiak6-Feb-24 12:22
mveDave Kreskowiak6-Feb-24 12:22 
GeneralRe: get_string memory leak? Pin
Mircea Neacsu6-Feb-24 12:28
Mircea Neacsu6-Feb-24 12:28 
AnswerRe: get_string memory leak? Pin
jschell6-Feb-24 5:02
jschell6-Feb-24 5:02 
mike7411 wrote:
I think it has a memory leak.


Despite the other posts - yes this would be a classic example of a memory leak.

The teardown() method, as used, is pointless. When the application exits (on every modern OS including small ones) the memory that the application uses is returned to the OS. So the fact that teardown is called on the application exit via the atexit() method is absolutely pointless.

If you were to use the code in a regular application, especially but not limited to one that does not exit, then this would fail. Because of memory leaks. You could not call teardown() arbitrarily either because it would clear everything.

Now it is possible that the point of the code is not to teach you efficient memory usage. But one could certainly argue that the get_string() method should make it clear that this is not a good way to do things.

However the following is in comment for that method.

Stores string
on heap, but library's destructor frees memory on program's exit.


And that indicates that the creator of this code did not understand/know what I pointed out above.
The code also has a copyright date of 2023 so there is no claim that the code could have been written, for example 50 years ago, where one might (perhaps) have used an OS where the application would have needed to do that. Note however that even then I am not sure any OSes actually worked that way. Back then PC-DOS, CPM and early unix versions (there was no linux) returned memory to the OS.

You can take from this that you should not write code like this.

In general in C and C++ you should always control the scope of your memory allocations. So for example if your method allocates some memory then that same method should deallocate (all exit conditions for the method included.) There should almost never be a need to have a method return an allocation and if it does happen then the method should document (comment) that the caller is responsible for the allocation.

C++ makes this easier since you can wrap the allocation in a class. And control allocation/deallocation from that. That is something that I would suggest always doing. So in C++ you would never return an allocated pointer.
GeneralRe: get_string memory leak? Pin
k50546-Feb-24 5:15
mvek50546-Feb-24 5:15 
GeneralRe: get_string memory leak? Pin
RedDk6-Feb-24 10:06
RedDk6-Feb-24 10:06 
GeneralRe: get_string memory leak? Pin
jschell7-Feb-24 5:22
jschell7-Feb-24 5:22 
GeneralRe: get_string memory leak? Pin
charlieg12-Feb-24 23:04
charlieg12-Feb-24 23:04 
Question"connect" with "lambda" C++ code analysis , please. Pin
Salvatore Terress3-Feb-24 15:01
Salvatore Terress3-Feb-24 15:01 
SuggestionRe: "connect" with "lambda" C++ code analysis , please. Pin
k50543-Feb-24 15:55
mvek50543-Feb-24 15:55 
GeneralRe: "connect" with "lambda" C++ code analysis , please. Pin
Salvatore Terress3-Feb-24 16:44
Salvatore Terress3-Feb-24 16:44 
GeneralRe: "connect" with "lambda" C++ code analysis , please. Pin
k50544-Feb-24 4:34
mvek50544-Feb-24 4:34 
GeneralRe: "connect" with "lambda" C++ code analysis , please. Pin
Salvatore Terress4-Feb-24 4:49
Salvatore Terress4-Feb-24 4:49 
GeneralRe: "connect" with "lambda" C++ code analysis , please. Pin
k50544-Feb-24 5:30
mvek50544-Feb-24 5:30 
GeneralRe: "connect" with "lambda" C++ code analysis , please. Pin
Salvatore Terress4-Feb-24 13:36
Salvatore Terress4-Feb-24 13:36 
GeneralRe: "connect" with "lambda" C++ code analysis , please. Pin
k50545-Feb-24 4:25
mvek50545-Feb-24 4:25 
GeneralRe: "connect" with "lambda" C++ code analysis , please. Pin
Salvatore Terress5-Feb-24 6:30
Salvatore Terress5-Feb-24 6:30 
GeneralRe: "connect" with "lambda" C++ code analysis , please. Pin
Richard MacCutchan4-Feb-24 6:16
mveRichard MacCutchan4-Feb-24 6:16 
GeneralRe: "connect" with "lambda" C++ code analysis , please. Pin
jschell5-Feb-24 5:33
jschell5-Feb-24 5:33 
GeneralRe: "connect" with "lambda" C++ code analysis , please. Pin
Salvatore Terress5-Feb-24 6:38
Salvatore Terress5-Feb-24 6:38 
GeneralRe: "connect" with "lambda" C++ code analysis , please. Pin
k50545-Feb-24 7:10
mvek50545-Feb-24 7:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.