Posts

Showing posts with the label unique-ptr

Unique_ptr: pointer being freed was not allocated when emplaced in list

Image
Clash Royale CLAN TAG #URR8PPP Unique_ptr: pointer being freed was not allocated when emplaced in list I have a simple C++ program like so: #include <iostream> #include <list> #include <memory> int main(void) { std::list<std::unique_ptr<int>> l; int x = 5; // throws runtime error: pointer being freed was not allocated l.emplace_back(&x); } When the program is run I obtain the following output: a.out(59842,0x7fffb13d1380) malloc: *** error for object 0x7ffee81489ac: pointer being freed was not allocated *** set a breakpoint in malloc_error_break to debug [1] 59842 abort ./a.out a.out(59842,0x7fffb13d1380) malloc: *** error for object 0x7ffee81489ac: pointer being freed was not allocated *** set a breakpoint in malloc_error_break to debug [1] 59842 abort ./a.out This tells me that during the destruction of the list the unique_ptr object that had been created is being destroyed, and during that destruction the pointer to x is bein...