R / igraph : any call to get/set vertex attribute within a depth-first-search callback causes a segfault

前提是你 提交于 2019-12-11 14:05:26

问题


I'm finding that any call to get/set vertex attribute using the igraph library within callback causes a segfault in R. For example, the trivial callback from a segment of code:

  dfsCallBack <- function(graph, data, extra) {
    cat("in:", paste(collapse=", ", data), "\n")
    distFromRoot <- data[2]
    vertexID <- data[1]
    set.vertex.attribute(graph, 0, name = 'color', value = 'blue')
    FALSE    
  }
  graph.dfs(g, 1, in.callback = dfsCallBack)

Produces this error:

  graph.dfs(g, 1, in.callback = dfsCallBack)
in: 0, 0 

 *** caught segfault ***
address 0x0, cause 'memory not mapped'

Traceback:
 1: .Call("R_igraph_dfs", graph, root, neimode, unreachable, as.logical(order),     as.logical(order.out), as.logical(father), as.logical(dist),     in.callback, out.callback, extra, rho, PACKAGE = "igraph")
 2: graph.dfs(g, 1, in.callback = dfsCallBack)

Possible actions:
1: abort (with core dump, if enabled)
2: normal R exit
3: exit R without saving workspace
4: exit R saving workspace
Selection: 

What's wrong here? Also, igraph should probably be more robust in dealing with these errors, crashing out of R is not ideal for a high-level language like R.


回答1:


A couple of points.

  1. If you say set.vertex.attribute(graph, ...), graph is actually not changed, but a new copy is returned with the attribute updated. R objects are (almost always) immutable, you cannot change them, only create new objects based on them.

  2. set.vertex.attribute(graph, 0, name = 'color', value = 'blue') is wrong, because vertex ids start at 1, so the 0 is invalid. This should be reported as an error, and was already fixed in our development tree.

  3. This is not an error, it is a bug. igraph errors do not crash R, they just give an error message. Because the igraph code and R run in the same thread, igraph bugs might crash R, and apart from avoiding bugs, there is not much we can do about this.

I would suggest to use the results of igraph.dfs to set the attributes appropriately. You can of course use the callback to record information, or to terminate the DFS.



来源:https://stackoverflow.com/questions/17684034/r-igraph-any-call-to-get-set-vertex-attribute-within-a-depth-first-search-ca

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!