Modify

Ticket #3546 (closed Bugs: worksforme)

Opened 4 years ago

Last modified 4 years ago

Regex: bizarre behaviour in basic_regex::do_assign with MSVC 2008

Reported by: Keith MacDonald <keith@…> Owned by: johnmaddock
Milestone: Boost 1.41.0 Component: regex
Version: Boost 1.40.0 Severity: Showstopper
Keywords: Cc:

Description

The attached program demonstrates the different behaviour, in release and debug modes, of basic_regex::do_assign when built with MSVC 2008 SP1. This can be observed in the following code, starting on line 696 of basic_regex.hpp:

   if(!m_pimpl.get())
   {
      temp = shared_ptr<re_detail::basic_regex_implementation<charT, traits> >(new re_detail::basic_regex_implementation<charT, traits>());
   }
   else
   {
      temp = shared_ptr<re_detail::basic_regex_implementation<charT, traits> >(new re_detail::basic_regex_implementation<charT, traits>(m_pimpl->m_ptraits));
   }

In release mode, both assignments to temp seem to be executed!

In order that the release code can be stepped through, it is necessary to build boost as follows:

bjam variant=release toolset=msvc link=static runtime-link=static threading=multi cxxflags=/Zi

The RegexBug? project assumes that the boost libraries are installed in C:\Dev\boost_1_40_0\bin.v2\libs, so the "Additional library directories" property will have to be modified if they are installed elsewhere. Then, create the debug and release builds, and put a breakpoint on line 696 of basic_regex.hpp. Run the debug build first, and when the breakpoint is reached, add "this" and "temp" to the Watch Window. Then, step over each line to observe the correct behaviour. Now do the same thing with the release build and note that the address of temp changes twice while stepping through the if statement.

Strangely, the subsequent call to regex_search succeeds in this sample code, but fails in my application. This is a complete showstopper for me, so I hope it can be easily fixed.

Attachments

RegexBug.zip Download (9.7 KB) - added by Keith MacDonald <keith@…> 4 years ago.
RegexBug? project files
REBugTest.zip Download (126.0 KB) - added by Keith MacDonald <keith@…> 4 years ago.
REBugTest application for MSVC 2008

Change History

Changed 4 years ago by Keith MacDonald <keith@…>

RegexBug? project files

comment:1 follow-up: ↓ 2 Changed 4 years ago by johnmaddock

  • Status changed from new to closed
  • Resolution set to worksforme

I believe that what you are seeing in basic_regex.hpp is an illusion: the debugger does jump around all over the place (including to lines that aren't executed) when optimisations are turned on. In any case all the regression tests pass for me here with both vc8 and vc9 in release mode. More likely there is some other problem (uninitialised memory?) lurking somewhere in your program that only manifests itself when you call the regex code.

Please do reopen though if you have a test case that I can look at here.

Changed 4 years ago by Keith MacDonald <keith@…>

REBugTest application for MSVC 2008

comment:2 in reply to: ↑ 1 Changed 4 years ago by Keith MacDonald <keith@…>

  • Status changed from closed to reopened
  • Resolution worksforme deleted

I couldn't get to the bottom of this problem, so went back to using 1.39.0, until I had more time to look into it. I've tried tried building up from the basic code that was failing, but that always worked, so have now approached it from the other end, by simplifying the application that failed. I've cut it down to the attached skeleton (REBugTest.zip), but still can't figure out why it's failing.

It works with 1.39.0, but only in debug mode with 1.40.0. In release mode, it starts going wrong on line 52 of regex_search.hpp:

]if(e.flags() & regex_constants::failbit)

The evaluation of e.flags() branches off into the depths various of the std libraries (eg. string or vector), depending on minor changes to the sample code. Thus, the above code returns true or false, depending on which bit of the std library was executed. If it returns false, an exception is subsequently thrown at line 56 of perl_matcher_common.hpp ("Invalid regular expression object").

Note that both debug and release builds have all optimisations disabled, so the only difference should be which boost library they automatically link to.

To reproduce the problem, create and run the release build, type "abc" in both the Find and Test edit controls, then press F3 (Edit/Test? Find).

I'd really appreciate some help with this - even if it's to tell me I'm doing something stupid!

comment:3 Changed 4 years ago by johnmaddock

Unfortunately I don't have access to MFC here, so I can't reproduce. The only thing I can think of to check is whether the CmdTestFind? object is valid when CmdTestFind::exec is called: try adding a sentry integer value to the object which gets initialised to a unique value when the object is constructed, and reset to zero when it is destroyed. Then you can check the value inside CmdTestFind::exec to verify it's being called for a valid object.

Otherwise the most likely cause is a binary compatibity issue: try adding the regex .cpp files (in boost-path/libs/regex/src) directly to your project, and then adding BOOST_REGEX_NO_LIB to the projects #defines.

If that doesn't work I'm stumped unless you can provide a console app that demonstrates the issue.

HTH, John.

comment:4 Changed 4 years ago by Keith MacDonald <keith@…>

Thanks for your suggestions.

In the sample code for REBugTest, I have already dispensed with the CmdTestFind? object, and put the calls to the regex stuff inline in FormReplace::OnCmdTestFind?. The FormReplace? object is created at startup and is only deleted when the GUI is closed.

I was going to suggest that you can download MFC with Visual Studio 2010 beta 2 from here:

 http://www.microsoft.com/visualstudio/en-gb/try/default.mspx

However, I thought I'd better check that it demonstrated the problem first. It doesn't!

On that basis, it seems that there is either a bug in the way VS 2008 compiles regex 1.40.0, or more likely in the way it links it in an application using MFC.

I'll try adding the regex .cpp files to the project and report back on that.

comment:5 Changed 4 years ago by Keith MacDonald <keith@…>

John, your suggestion of adding the regex .cpp files to the project has fixed the problem. It wasn't necessary to define BOOST_REGEX_NO_LIB (presumably because there's nothing to resolve from that library).

Many thanks for the workaround. I hope it's not necessary with future releases of Boost.

  • Keith

comment:6 Changed 4 years ago by johnmaddock

  • Status changed from reopened to closed
  • Resolution set to worksforme

I'm closing this issue down as it appears to be build or MSVC related, rather than a regex issue.

Regards, John.

View

Add a comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
The resolution will be deleted. Next status will be 'reopened'
Author


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

 
Note: See TracTickets for help on using tickets.