cyang-kth / fmm

Fast map matching, an open source framework in C++
https://fmm-wiki.github.io/
Apache License 2.0
878 stars 205 forks source link

It will cause the program to fail that putting the config initialization process into the function #199

Closed mchl0203 closed 3 years ago

mchl0203 commented 3 years ago

I try to define config initialization as a function, and running the program will cause a error. code:

from fmm import Network,NetworkGraph,STMATCH,STMATCHConfig

def init_model():
    network = Network("../data/edges.shp")
    print("Nodes {} edges {}".format(network.get_node_count(),network.get_edge_count()))
    graph = NetworkGraph(network)
    model = STMATCH(network,graph)

    k = 4
    gps_error = 0.5
    radius = 0.4
    vmax = 30
    factor = 1.5
    stmatch_config = STMATCHConfig(k, radius, gps_error, vmax, factor)
    return model, stmatch_config

wkt = "LINESTRING(0.200812146892656 2.14088983050848,1.44262005649717 2.14879943502825,3.06408898305084 2.16066384180791,3.06408898305084 2.7103813559322,3.70872175141242 2.97930790960452,4.11606638418078 2.62337570621469)"
m, c = init_model()
result = m.match_wkt(wkt, c).mgeom.export_wkt()
print(result)

error: Segmentation fault (core dumped)

I tried to use py2 and py3 to run the code, but the program will report errors. Could you trouble me to troubleshoot this error?very thankful~

cyang-kth commented 3 years ago

You cannot wrap the Network, NetworkGraph objects inside a function. Previously there has been a similar issue but I cannot find it now.

The reason is that python binding is created using swig so the API has some differences from most other Python API you have used. The stmatch model is created by taking reference rather than copying the inner network and graph object. In your function, the network and graph objects are freed when the function finishes. Then the model links to some non-exist network and graph objects so you encounter this error.

You need to put everything in the global scope and avoid this way of returning only a single model.

In the future, maybe every object could be defined as shared pointer to support this way of invoking.

mchl0203 commented 3 years ago

Okay, thank you for your detailed reply.