Unable to run an Octave script from C++ code

Thank you for writing us about your problem with using :octave: GNU Octave. To help us help you, please answer the following questions, if applicable.

Problem description:
I developed the code using Octave 4.x and it worked very fine. After the upgrade to Octave 6.x I received a segmentation fault error when I use the C++ call

const octave_value_list result=octave::feval("em_sample",functionArguments, 1);

I set the OCTAVE_PATH env variable correctly to allow Octave to find the script em_sample.m

functionArguments (0) = statistical_DB; where statistical_DB is a long string containing data needed to em_sample
I would expect:
init_file = result(0).string_value(); where init_file contains initial data for the C++ procedure
dyn_file = result(1).string_value(); where dyn_file contains dynamic data for the C++ procedure
They are needed to build aircraft trajectories to simulate aircraft encounters and test Collision Avoidance Systems (like TCAS or ACAS). It wirked realyy fine with octave 4.x and now I cannot recover this error. Any ideas? It’s really very critical for me
Thanks a lot

First, if you’re pasting code please use the backquotes or backticks to fence off your code otherwise formatting is a mess.

Second, your problem sounds similar to GNU Octave - Bugs: bug #62308, use of octave::feval causes error:... [Savannah]

Is that the case?

1 Like

OK, I reduced my code to the essential and the error seems to be what you mention. I read the Bug report, but, as far as I understood, no remedies are suggested. Do i have to wait for a new release correcting the bug?

The fix has been incorporated in the source and will be released in 7.2, which is supposed to happen soon, but if you need it right away you can download the 7.1.90 version (that’s the pre-7.2 version) from GNU Alpha: Index of /gnu/octave

If you download and try 7.1.90, please let us know if the problem is solved for you with that version.

OK thanks. I’m running 6.4.0-1 installed from fedora 35 repository. I have installed also many add-on octave packages. What do you suggest: uninstall only the basic Octave package leaving all the other and than install release 7.2 from source? All the other add-ons still continue to work?

It will probably be safer for you to install 7.1.90 separately somewhere in your home directory (not system-wide). That way you won’t have to uninstall the system Octave from your distribution repository and if you need to undo the change it’ll be easier.

Once you invoke the new Octave, it will read the package configuration files and you should be able to see the existing packages with pkg list.

As always, it’s best to make a backup archive of your package directory for peace of mind. That way if anything breaks you can delete the whole directory and restore from backup.

If you can do pkg update on Octave 6 first, you’ll probably have an easier time switching from Octave 6 to 7. The only package breakage that has been reported is you may run into problems with spaces in expressions like “x ++” which was legal for incrementing in Octave 6 but not in Octave 7. New versions of packages won’t have that problem and will work properly with Octave 7 so it’s best to upgrade packages to their latest versions before upgrading Octave, otherwise older packages with such spaces might break.

Bug #62308 is about an issue with Octave 7.1.0 (not 6.x). If you are using Octave 6, you are likely seeing something different.
What is the exact version of Octave you are using?
Does the function work correctly if you run it directly in Octave (as opposed to your C++ code)?
What does that function look like?

Edit: You write the em_sample is a script. Scripts don’t have output arguments.

1 Like

Markus,
sorry but I’ve seen your mail just now.
The Octave version I’m using is 6.4.0
The function works properly if run from Octave. I cannot provide you a copy because it comes from Stanford University and I cannot publish it without their consensus. Now I try to install version 7.1.90 from source code and test its behaviour. I’ll keep you informed.
Thanks to everybody for supporting me
Andrea

Thanks for coming back to this.
If Octave 7.1.90 still crashes, could you please provide a gdb backtrace from the segmentation fault?

For sure.
Thanks a lot lot for supporting me

Hi to everybody.
OK, I followed the following steps:

  1. I minimize my application in order to better focalize the problem. The application prepares the input data for the Octave script, call the feval instruction, and then process the output data from the Octave script. I re-compiled it with Octave 6.4.0 and it presents the same error “error encountered in Octave evaluator!”

  2. I downloaded Octave 7.1.90 source files, and compiled it but I didn’t installed it, identifying the libraries and includes files needed

  3. In my application I substituted the references of the relevant include file from version 6.4.0 to version 7.1.90

  4. In the Makefile I substituted the relevant libraries from version 6.4.0 to version 7.1.90
    but the compilation phase gave me a lot of error, too many to understand the issues inside the Octave package.
    Maybe the way of using the feval function is totally different in version 7.1.90.
    The first error is related to a name space
    “defun-int.h:39:1: error: ‘OCTAVE_NAMESPACE_BEGIN’ does not name a type
    39 | OCTAVE_NAMESPACE_BEGIN”
    Them there are a lot of other errors like

“error: ‘check_version’ is not a member of ‘octave’; did you mean ‘check_version’?
85 | octave::check_version (version, fcn);”
that seems something related to namespaces definition…
Any suggestions)
Thanks a lot
Andrea

Well, some improvements. Please forget the previous e-mail. I didn’t install octave libs correctly.
I achieved some results. Hope to be able to explain it. As a first step I will show you the code snippet of interest.

functionArguments(0) = statistical_DB;
const octave_value_list result=octave::feval(“em_sample”,functionArguments, 1);
// capture the results from Octave (initial and transition distribution)
init_file = result(0).string_value();
dyn_file = result(1).string_value();

Some explanation:
statistical_DB is a very long string read from a file containing the data input for the octave procedure em_sample
init_file and dyn_file are two additional strings generated by the octave script em_sample.
Well, init_file is well calculated, that means that the procedure em_sample is correctly evaluated, but the program crashes on dyn_file (segmentation fault).

I try to copy the debug result

"114 functionArguments(0) = statistical_DB;
(gdb)
115 const octave_value_list result=octave::feval(“em_sample”,functionArguments, 1);
(gdb) n
117 init_file = result(0).string_value();
(gdb) n
118 dyn_file = result(1).string_value();
(gdb) p init_file
$8 = “1 4 1 1 207.75 2 1 110.738 149.782 0 0.895019 0 0 1.8793 0 2.28865 353.626 \n”
(gdb) n

Program received signal SIGSEGV, Segmentation fault.
octave_value::string_valueabi:cxx11 const (this=0x86d1c8, force=false) at /usr/local/include/octave-7.1.90/octave/ov.h:1020
1020 { return m_rep->string_value (force); }"

Hope this will be useful

Andrea

It looks to me like you are calling feval incorrectly. The last parameter to the function is the number of expected output arguments. The C++ code is

  //! Evaluate an Octave function (built-in or interpreted) and return
  //! the list of result values.
  //!
  //! @param name The name of the function to call.
  //! @param args The arguments to the function.
  //! @param nargout The number of output arguments expected.
  //! @return A list of output values.  The length of the list is not
  //!         necessarily the same as @c nargout.

  octave_value_list interpreter::feval (const char *name,
                                        const octave_value_list& args,
                                        int nargout)

Could you try changing 1 to 2 in the call to feval and see if that helps?

1 Like

Well, I have to thank you a lot.
All now is working properly.
Version 7.1.90 is working as expected.
Thank you very much
Andrea

1 Like