Reputation: 121
I have code like this, when class A
manages an object B
and vector<shared_ptr<B>>
, then I create a A
from a static method, when I do this, I think if A::returnA()
goes out of scope, then A::returnA()
will be deleted, then B
that's managed by A
is deleted, then vector<shared_ptr<B>>
is deleted.
However, when I run this code, the memory usage displayed in monitor is huge, and continue increasing, I don't know why this is leaking memory.
#include <vector>
#include <memory>
#include <iostream>
using namespace std;
class A {
public:
static A returnA(){
return A();
}
class B {
public:
~B() { cout << "~B" << endl; }
};
B b;
vector<std::shared_ptr<B>> v;
A(){
std::unique_ptr<B> b2 = make_unique<B>();
v.emplace_back(new B());
};
~A() { cout << "~A" << endl; }
};
int main() {
while(1){
A::returnA();
}
}
Upvotes: 0
Views: 298
Reputation: 26395
Your screenshot does not show the memory of your app. It shows the memory of Xcode. It's Xcode that's taking up so much memory, possibly because it's keeping the log of cout
so you can scroll through it.
Upvotes: 3