0

I'm trying to make an observable that when an error is detected, this will be execute again, but did notice something , when "on_error()" with "retry" operator is execute, this only re-run again the Observable but, the current instance of Observable still in the current stack, in other words is still alive

I did make a test to verify the behavior

#include <string>
#include "rxcpp/rx.hpp"

class test_class 
{
public:
    int a;
    test_class() {
       printf("Create Obj \n");
       a = 1;
    }
   ~test_class() {
       printf("Destroy Obj \n");
       a = 0;
   }
};

int main()
{
   // Create Observable request
   auto values = rxcpp::observable<>::create<std::string>(
        [&](rxcpp::subscriber<std::string> subscriber) {
           test_class test;
           while (subscriber.is_subscribed()) {
               std::exception_ptr eptr = std::current_exception();
               subscriber.on_error(eptr);
               int a;
               a = 2;
               subscriber.on_next("normal");
           }

     })
    .retry()
    .as_dynamic();


values.
    subscribe(
        [](std::string v) {
                          printf("OnNext: %s\n", v.c_str()); },
        [](std::exception_ptr ep) {
                printf("OnError: %s\n", rxcpp::util::what(ep).c_str()); },
        []() {
                    printf("OnCompleted\n"); });

}

So, my input output is

Create Obj
Create Obj
Create Obj
Create Obj
...

I did expect to see "Destroy Obj" output as well

also I got a Stack overflow exception

enter image description here

My goal is , execute an Observable Object, that when an error is triggered, this could be restart again, but destroying curruent one, in orden to prevent Stack overflow exception

Maybe there's exist another way to make this, could you help me?

Whiso
  • 236
  • 2
  • 4
  • The solution here is to use the scheduler. Try adding observe_on using the current_thread scheduler between the create and retry. current_thread is used to protect the stack on the current thread. – Kirk Shoop Apr 22 '19 at 14:44

1 Answers1

0

I found a possible solution, I only remove the loop inside Observable and retry operator, then I add a loop in Subscribe operation

I know is not an "Elegant" solution but that is the idea that I want to do, could you help me on this? How could be the better way using RxCPP library?

#include <string>
#include "rxcpp/rx.hpp"

class test_class
{
   public:
   int a;
   test_class() {
     printf("Create Obj \n");
     a = 1;
   }
   ~test_class() {
     printf("Destroy Obj \n");
     a = 0;
}
};

int main()
{
   // Create Observable request
   auto values = rxcpp::observable<>::create<std::string>(
        [&](rxcpp::subscriber<std::string> subscriber) {
        test_class test;
        //while (subscriber.is_subscribed()) {
             std::exception_ptr eptr = std::current_exception();
             subscriber.on_error(eptr);
             int a;
             a = 2;
             subscriber.on_next("normal");
        //}
   });
   //.retry()
   //.as_dynamic();

   for (;;) {
      values.
          subscribe(
              [](std::string v) {
                  printf("OnNext: %s\n", v.c_str()); },
              [](std::exception_ptr ep) {
                  printf("OnError: %s\n", rxcpp::util::what(ep).c_str()); },
              []() {
                  printf("OnCompleted\n"); });
   }
}

Here my output:

Create Obj
OnError: bad exception
Destroy Obj
Create Obj
OnError: bad exception
Destroy Obj

Without stack overflow exception error

Whiso
  • 236
  • 2
  • 4