Skip to content

Instantly share code, notes, and snippets.

@momchil-velikov
Last active November 8, 2015 09:52
Show Gist options
  • Save momchil-velikov/a8c9f86ffd60cd493457 to your computer and use it in GitHub Desktop.
Save momchil-velikov/a8c9f86ffd60cd493457 to your computer and use it in GitHub Desktop.
Leaking memory with "smart" pointers
#include <memory>
#include <vector>
#include <algorithm>
#include <array>
struct data {
data(char c) { std::fill(begin(blk), end(blk), c); }
std::array<char, (1 << 20)> blk;
};
int main() {
std::vector<std::weak_ptr<data>> v;
for (int i = 0; i < (1 << 14); i++) {
#if 0
std::shared_ptr<data> sp = std::make_shared<data>('a');
#else
std::shared_ptr<data> sp(new data('a'));
#endif
v.push_back(sp);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment