Modify

Opened 2 years ago

Last modified 2 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';

}

Attachments (0)

Change History (6)

comment:1 Changed 2 years ago by anonymous

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

comment:2 Changed 2 years ago by gunjan.rani100@…

In this case segmentation fault occurred with few other compilers.

comment:3 Changed 2 years ago by gunjan.rani100@…

Version: Boost 1.61.0Boost 1.57.0

comment:4 Changed 2 years ago by Steven Watanabe

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

comment:5 Changed 2 years ago by viboes

Component: Nonesmart_ptr
Owner: set to Peter Dimov

comment:6 Changed 2 years ago by anonymous

Why there is no exception handling for scoped_ptr creation?

Modify Ticket

Change Properties
Set your email in Preferences
Action
as new The owner will remain Peter Dimov.

Add Comment


E-mail address and name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.