Memmory related failures in smile engine

The engine.
Post Reply
sverrevr
Posts: 26
Joined: Mon Aug 24, 2020 7:37 am

Memmory related failures in smile engine

Post by sverrevr »

Hi, I'm experiencing a weird bug in the smile library for c++ in the following case:
I have a network consisting of at least one temporal node
"simple.xdsl"

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<!-- This network was created in GeNIe Academic, which can be used for academic teaching and research purposes only -->
<smile version="1.0" id="Network3" numsamples="10000" discsamples="10000">
	<nodes>
		<cpt id="Node2" dynamic="plate">
			<state id="State0" />
			<state id="State1" />
			<probabilities>0.9 0.1</probabilities>
		</cpt>
	</nodes>
	<dynamic numslices="10">
	</dynamic>
	<extensions>
		<genie version="1.0" app="GeNIe 4.0.2221.0 ACADEMIC" name="Network3">
			<plate leftwidth="120" rightwidth="120">69 65 669 365</plate>
			<node id="Node2">
				<name>Node2</name>
				<interior color="e5f6f7" />
				<outline color="000080" />
				<font color="000000" name="Arial" size="8" />
				<position>314 256 362 286</position>
			</node>
		</genie>
	</extensions>
</smile>
If I'm using a wrapper class for the DSL_network (see the class BayesianNetwork below), and I have instances of that class in a container, such as a vector or map, then I get a memory-related error.

To demonstrate that both the wrapper class and the container are needed, the following example code has first one case where DSL_network is directly in a container, then the wrapper class is used without the container, before the wrapper is used with the container

Code: Select all

#include "smile/smile.h"
#include "smile/smile_license.h"
#include <vector>
#include <iostream>


void direct_in_a_vector(){
    std::vector<DSL_network> nets;
    nets.push_back(DSL_network());
    nets[0].ReadFile("simple.xdsl");
    nets[0].UpdateBeliefs();
}

class BayesianNetwork{
    private:
    DSL_network net;

    public:
    BayesianNetwork(){
        net.ReadFile("simple.xdsl");
    }
    void update(){
        net.UpdateBeliefs();
    }
};

void in_a_class(){
    BayesianNetwork net;
    net.update();
}

void class_in_vector(){
    std::vector<BayesianNetwork> nets;
    nets.push_back(BayesianNetwork());
    nets[0].update();
}

int main(){
    DSL_errorH().RedirectToFile(stdout);
    direct_in_a_vector();
    std::cout << "3\n" << std::flush;
    in_a_class();
    std::cout << "2\n" << std::flush;
    class_in_vector();
    std::cout << "1\n" << std::flush;
}
Compiling that code with:

Code: Select all

clang++ -std=c++17 -DNDEBUG -O3 test.cpp -o test -I./smile -L./smile -lsmile
Then running it gives:

Code: Select all

3
2
terminate called after throwing an instance of 'std::length_error'
  what():  vector::_M_default_append
Aborted
Note that sometimes I get the following instead:

Code: Select all

3
2
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc
I cant see a system in when each error occurs

Compiling it with g++ instead has similar behavior:

Code: Select all

g++ -std=c++17 -DNDEBUG -O3 test.cpp -o test -I./smile -L./smile -lsmile

Code: Select all

3
2
Killed
or sometimes:

Code: Select all

3
2
Segmentation fault

The error does not happen if the node is not dynamic/temporal.

I experienced this bug when I made a new project that was based on an earlier one. The same configuration of having a wrapper class and using a container was in both. So I believe that this has worked in either an older version of the library or a different build.



The following build of the smile library is used:
SMILE ACADEMIC version 2.0.8 / 2022-07-29

I'm pretty sure I have the following version.
smile-academic-2.0.8-linux-x64-gcc-7.5

I'm running:
Ubuntu 18.05.6 LTS on wsl on windows 10
gcc (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0
g++ (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0
clang version 6.0.0-1ubuntu2 (tags/RELEASE_600/final)
Last edited by sverrevr on Tue Oct 25, 2022 11:54 am, edited 1 time in total.
sverrevr
Posts: 26
Joined: Mon Aug 24, 2020 7:37 am

Re: Memmory related failures in smile engine

Post by sverrevr »

I found an older version of the library in an older project I had.
With this version, it worked without any errors.

Version:
SMILE ACADEMIC version 1.6.0 / 2020-09-28
The rest is identical as in the original post
shooltz[BayesFusion]
Site Admin
Posts: 1417
Joined: Mon Nov 26, 2007 5:51 pm

Re: Memmory related failures in smile engine

Post by shooltz[BayesFusion] »

We were able to reproduce this using a different compiler, so it's clearly a SMILE 2.0.x bug. I'll send you a link to a private build once the fix is ready.
sverrevr
Posts: 26
Joined: Mon Aug 24, 2020 7:37 am

Re: Memmory related failures in smile engine

Post by sverrevr »

Thank you :)
shooltz[BayesFusion]
Site Admin
Posts: 1417
Joined: Mon Nov 26, 2007 5:51 pm

Re: Memmory related failures in smile engine

Post by shooltz[BayesFusion] »

I sent an email with a download link to the account you're using on this forum.
sverrevr
Posts: 26
Joined: Mon Aug 24, 2020 7:37 am

Re: Memmory related failures in smile engine

Post by sverrevr »

The patched version solved the problem in the minimal example above, but it still causes a "segmentation fault" on my main project when calling UpdateBeliefs(). With the older version, it still works on my main project.
I'm sorry that I don't have time to make a new minimal example in the near future
shooltz[BayesFusion]
Site Admin
Posts: 1417
Joined: Mon Nov 26, 2007 5:51 pm

Re: Memmory related failures in smile engine

Post by shooltz[BayesFusion] »

If you can, send me a link or a .zip with the complete project to my email address. We'll try to reproduce/fix the problem.
Post Reply