potassco / clingo

🤔 A grounder and solver for logic programs.
https://potassco.org/clingo
MIT License
601 stars 79 forks source link

Segfault using python interface #395

Closed damianoazzolini closed 1 year ago

damianoazzolini commented 1 year ago

Hi, I'm testing some programs and get the following error when using the Python interface

free(): double free detected in tcache 2
Aborted

So i've tracked down the problem and I got this minimal example:

import clingo

asp_program = ["{a}. {b}."]

ctl = clingo.Control(["--models=0"])
for clause in asp_program:
    ctl.add('base', [], clause)

ctl.ground([("base", [])])

opt = " "
with ctl.solve(yield_=True) as handle:
    for m in handle:
        opt = m
        print(opt)
    handle.get()

print(opt) # <---- this

Note that if I remove the last print (denoted with <---), the program works well and no errors are thrown. Note also that if I remove the print inside the for loop the program works as well.

> clingo --version
clingo version 5.5.2
Address model: 64-bit

libclingo version 5.5.2
Configuration: with Python 3.9.12, without Lua

libclasp version 3.3.8 (libpotassco version 1.1.0)
Configuration: WITH_THREADS=1
Copyright (C) Benjamin Kaufmann

Honestly, I'm not sure whether this is problem is from clingo or Python itself...

rkaminsk commented 1 year ago

You cannot store model objects for later use. They are just references to the current model in the solver. But you can store its symbols, etc.

damianoazzolini commented 1 year ago

Got it, thanks.