user1876942
user1876942

Reputation: 1501

Memory leak with void*

How can I stop this memory leak.

I have created a vector like this in the header:

static std::vector< uint32_t> m_Map;
void* m_mapRegion

Then in the .cpp

m_Map.resize( 450 );
m_mapRegion = &m_map[0];

I then use m_mapRegion to access the vector via the address and all is OK. When I delete the object then the test tools flag up a memory leak. How can I get rid of the error? Thanks.

I tried this:

m_Map.clear();
m_mapRegion = NULL;

What is the correct way?

The error message given is:

HWTests.cpp:188: error: Failure in TEST(HW, Test)
Memory leak(s) found.
Alloc num (653) Leak size: 2400 Allocated at: <unknown> and line: 0. Type: "new"
 Memory: <0x11b2a70> Content: ""
Total number of leaks:  1

In the Test all I do is this:

HW HWAccessor;  //This line causes the error.

Upvotes: 1

Views: 319

Answers (2)

Ajay
Ajay

Reputation: 18431

vector::clear may not necessarily free up the memory in heap. It merely cleans up the vector, so that size would report 0. The vector must be destroyed, or std::swap with another temporary vector. The temporary should cease to exist soon.

{ 
  vector<uint32_t> temp;
  std::swap(temp, m_Map);
}

Upvotes: 0

nvoigt
nvoigt

Reputation: 77304

You never actually called new to dynamically allocate memory, so calling delete is not necessary.

Upvotes: 3

Related Questions