Opened 7 years ago

Last modified 7 years ago

#11879 new Bugs

Incorrect use of reset cause unexpected impact on previous code segment

Reported by: anonymous Owned by: Peter Dimov
Milestone: To Be Determined Component: smart_ptr
Version: Boost 1.57.0 Severity: Problem
Keywords: Cc:

Description

#include <boost/scoped_ptr.hpp> #include <iostream>

int main() {

boost::scoped_ptr<int> p{new int{1}}; std::cout << *p << '\n'; std::cout << p.get() << '\n'; p.reset(new int{2}); std::cout << *p.get() << '\n'; std::cout << p.get() << '\n';

p.reset((int *)4); Problem: Because of this statement std::cout of above lines are not printing anything. When this line is commented the program works fine. I understand I have used reset function incorrectly but it should impact to the next statements but it is also impacting above statements too. Please explain the cause.

std::cout << *p.get() << '\n'; std::cout << p.get() << '\n';

p.reset(); std::cout << std::boolalpha << static_cast<bool>(p) << '\n';

}

Change History (6)

comment:1 by anonymous, 7 years ago

I forgot to use wiki formatting. Comment start is missing from the statement p.reset((int *)4);

comment:2 by gunjan.rani100@…, 7 years ago

In this case segmentation fault occurred with few other compilers.

comment:3 by gunjan.rani100@…, 7 years ago

Version: Boost 1.61.0Boost 1.57.0

comment:4 by Steven Watanabe, 7 years ago

std::cout is buffered. You'll get the result you expect, if you add a flush before the incorrect code.

comment:5 by viboes, 7 years ago

Component: Nonesmart_ptr
Owner: set to Peter Dimov

comment:6 by anonymous, 7 years ago

Why there is no exception handling for scoped_ptr creation?

Note: See TracTickets for help on using tickets.