0

The application want to parse a string equation to mathematics and return the data to user. for this purpose the library is used is exprtk

for easy analysis I have shared minimum working code minimum working code

when application parses the string to code back to back [multithreaded but locked]

void reset()
{
      // Why? because msvc doesn't support swap properly.
      //stack_ = std::stack<std::pair<char,std::size_t> >();
      /**
      it was crashing on destructor on ~deque()
      stating memory reallocation
      so I change it to pop so for now this has been resolved
      */
      while(stack_.size()) stack_.pop();
      state_ = true;
      error_token_.clear();
}

now the code always crashes on

static inline void destroy(control_block*& cntrl_blck)
{
   if (cntrl_blck)
    {
        /**now crashes on this condition check*/
        if ( (0 !=   cntrl_blck->ref_count) &&  (0 == --cntrl_blck->ref_count) )
        {
           delete cntrl_blck;
        }

          cntrl_blck = 0;
     }
  }

UPDATE pastebin code updated new code with main has been added with main and minimum working code. all the shared_ptr has been removed. now they are normal objects. as for exprtk reset function has been changed to original one

void reset()
{
     // Why? because msvc doesn't support swap properly.
     stack_ = std::stack<std::pair<char,std::size_t> >();
     state_ = true;
     error_token_.clear();
}

and backtrace of gdb has been added backtrace

  • 1
    Where in *your* code does the crash happen? Can you please create a [mcve] that replicates the problem, and show it to us? And if you haven't done it yet, then please read about [how to ask good questions](http://stackoverflow.com/help/how-to-ask), as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). – Some programmer dude May 14 '19 at 10:11
  • Code provided is not runnable, needs a `main` function etc. And ideally use `std::mutex` etc. not pthreads (especially after mentioning MSVC. also unique_lock/scoped_lock etc. avoids manual resource management). I would guess it is a memory error where something got deleted. There is a lot of `shared_ptr` that seems to just be adding complexity/confusion. If it can be a stack/automatic variable, do that. If it can be `unique_ptr` do that. The piece of code in the question is not in the paste bin... But I would definitely avoid the manual `delete cntrl_blck` if any smart ptr can work. – Fire Lancer May 14 '19 at 10:24
  • @FireLancer I would say that the `destroy` function shown in the question is from a library, and not part of the actual code written by the OP. As is the `reset` function. – Some programmer dude May 14 '19 at 10:26
  • @Someprogrammerdude Good call, can confirm `destroy` is in exprtk.hpp but seems to be re-formatted so a simple search didn't find it. I think OP modified the `reset`, code on github assigns a fresh object as commented out. – Fire Lancer May 14 '19 at 10:33
  • @FireLancer both `reset` and `destroy` are library function. as for mutex . I am running in unix environment with gcc4.9 – Sujit chourasia May 14 '19 at 11:35
  • @Someprogrammerdude minimal code is shared – Sujit chourasia May 14 '19 at 11:35
  • I had to change some code. printf `%s` doesn't take a `std::string` or `double`, `get_value` didn't exist so used `value`, and syntax error around `for_each`. Compiled and no crash on my GCC 7.4 with Ubuntu 18.04. – Fire Lancer May 14 '19 at 12:33
  • @Fire Lancer standalone project is working for me also. But as soon as I merge in main project. It started crashes. Not a thing has been modified in standalone project. After seeing backtrace only thing I understood is its memory problems. – Sujit chourasia May 15 '19 at 04:00
  • Maybe try with Valgrind if it picks up any memory error. – Fire Lancer May 15 '19 at 08:48

0 Answers0