Yep, that looks like a memory leak.
If you don't want to explicitly call delete
on dynamically allocated memory, the standard solution is to use a smart pointer, which generally uses "reference counting" in order to de-allocate the memory safely.
More info on smart pointers: What is a smart pointer and when should I use one?
As for why you didn't allocate more memory -- what are you measuring? There are generally two "allocations" that happen:
- Your memory allocator requests memory from the operating system.
- Your memory allocator returns a pointer to some of that memory.
(Strictly speaking, this is not specific to your compiler, this is based on the implementation of new
. If you're using the standard Visual C++ toolchain, then in that sense, yeah, the implementation of new
depends on Visual C++. Here's a starting point: https://learn.microsoft.com/en-us/cpp/standard-library/memory?view=vs-2019)
It's possible that you're measuring #1 -- how much memory is being allocated by the operating system. For instance, sizeof(obj)
may be 1 kilobyte, and your allocator may have requested 256K from the operating system. So you have space to perform new
and receive allocations within that memory buffer, without changing the OS-level memory footprint of your process.
As an experiment, I'd suggest allocating successively larger chunks of memory:
const int alloc_size = 4; // then 5, 6, 7...
for (int i = 0; i < (2<<alloc_size); i++) {
// do your allocation here
}
and then finding out how many allocations you can perform before your measurement changes.