Why does memory leak in one case and not in another

how to fix a memory leak
memory leak c++
how to avoid memory leak in java
memory leak javascript
memory leak detection
memory leak c#
memory leak python
memory leak android

I am creating a c++ object with two slightly different ways, in the following code when CASE is 0 there is a memory leak, but no memory leak in the else case.

#include <string>
#define CASE 1

class A {
  std::string *s;
  A(std::string *p_s) { s = p_s; }

int main() {
#if CASE==0
  auto a = A(new std::string("Hello"));
  auto s = std::string("Hello");
  auto a = A(&s);

when I set CASE 0 the valgrind says that there is a memory leak

valgrind ./a.out 
==24351== Memcheck, a memory error detector
==24351== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==24351== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==24351== Command: ./a.out
==24351== HEAP SUMMARY:
==24351==     in use at exit: 32 bytes in 1 blocks
==24351==   total heap usage: 2 allocs, 1 frees, 72,736 bytes allocated
==24351== LEAK SUMMARY:
==24351==    definitely lost: 32 bytes in 1 blocks
==24351==    indirectly lost: 0 bytes in 0 blocks
==24351==      possibly lost: 0 bytes in 0 blocks
==24351==    still reachable: 0 bytes in 0 blocks
==24351==         suppressed: 0 bytes in 0 blocks
==24351== Rerun with --leak-check=full to see details of leaked memory
==24351== For counts of detected and suppressed errors, rerun with: -v
==24351== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

in the else case (i.e. define CASE 1) it works as expected and valgrind doesn't report any memory leak.

I am not able to understand in either case I am passing a pointer and I am not explicitly freeing the memory then why do they behave differently?

Memory leak, Each time this case occurs, more memory is leaked. People do not often press the button for the floor they are already move to another floor (such as when an attempt is made to call the  in the else case (i.e. define CASE 1) it works as expected and valgrind doesn't report any memory leak. I am not able to understand in either case I am passing a pointer and I am not explicitly freeing the memory then why do they behave differently?

The reason for this behavior is that your class A is not designed to take ownership of std::string* passed into it: its std::string *s member assumes that the object the pointer to which is passed into the constructor would be destroyed externally.

This leads to a memory leak when the object is not destroyed: delete is never called on new string passed into the constructor in the first case, causing a memory leak.

In the second case the pointer points to a string in automatic storage. It gets destroyed when main ends, preventing the memory leak.

Understanding Memory Leaks in Java, Learn what memory leaks are in Java, how to recognize them at runtime, And if not dealt with, the application will eventually exhaust its There are two different types of objects that reside in Heap memory — referenced and unreferenced. This can even happen in case of an exception that prevents the  In computer science, a memory leak is a type of resource leak that occurs when a computer program incorrectly manages memory allocations in such a way that memory which is no longer needed is not released. A memory leak may also happen when an object is stored in memory but cannot be accessed by the running code. A memory leak has symptoms similar to a number of other problems and generally can only be diagnosed by a programmer with access to the programs' source code. A space leak occurs when a

You don't get a memory leak because you have a pointer.

You get a memory leak because you new'd something and did not delete it.

Obtaining a pointer to an automatic storage variable does not stop the variable from being cleaned up automatically.

In fact, attempting to delete &a in that case would be wrong/broken/evil/illegal/heresy.

A dynamic approach to detecting, eliminating and fixing memory leaks, If input is assigned -1, the other path will be executed. In this case, no functions are executed and there exists no memory leak. This motivating  A memory leak is a process in which a program or application persistently retains a computer's primary memory. It occurs when the resident memory program does not return or release allocated memory space, even after execution, resulting in slower or unresponsive system behavior. A memory leak is also known as a space leak.

This is no different from:

// first case, leak
int *j = new int (5);
// second case, no leak
int q = 5;
int *j = &q;

In the first case, we've allocated memory with new and it's our responsibility to delete it when we're done. In the second case, we create q on the stack and it's destroyed when it goes out of scope.

Using Memory Leak Checking (Sun Studio 12: Debugging a , A memory leak is a dynamically allocated block of memory that has no pointers pointing Runtime checking does not report a block as a leak, if the program still retains one or more pointers to it. In all other cases, it is likely to be a real leak. However, despite having adequate RAM and not running resource-intensive software, there can be another situation where all available RAM gets used and performance degrades. This is known as a

Understand and Prevent Memory Leaks in a Java Application, Memory leaks are a very real problem in Java and the JVM can only The standard definition of a memory leak is a scenario that occurs when objects are no longer In this case, the BufferedReader will be automatically closed at the In this article, we used another profiler – YourKit – which has some  In this case, a memory leak is quite literal, and fairly hazardous. The other theory comes from the mainframe days. Memory was shared between any running jobs and was called a "pool" of memory after things like motor pools, secretarial pools, and similar. When a job (program) caused memory to become inaccessible, that memory "leaked" out of the pool.

Clarion Tips & Techniques, So here goes . . . ! make another dc just like the one you have ! make a hmembitmap ) Since the memory DC has no physical device associated with it , I a default everything , so when a new something , in this case bitmap , is put in Of course , make that mistake in an inner loop and it becomes a major memory leak ! The memory leak occurs when the binding mode is OneWay or TwoWay. If the binding is OneTime or OneWayToSource, it’s not a problem. Another WPF memory leak issue occurs when binding to a collection. If that collection doesn’t implement INotifyCollectionChanged, then you will have a memory leak.

Debug Leaky Apps: Identify And Prevent Memory Leaks In Managed , Maybe memory use is gradually (or not so gradually) increasing in Task Manager​. Interoperating with Unmanaged Code One source of memory leaks On other runs of the application, this is not the case and the memory  In this case there is a room leak. If your program allocates memory and does not delete it (it merely stops using it) then the computer thinks that the memory is still in use and will not allow anyone else to use it. This is a memory leak. This is not an exact analogy but it might help.

  • In the first case, you allocate some memory with new, so you must release it with delete. In the second case, you just take a pointer to an object on the stack - no need to delete it.