What is the reason of Segmentation Fault in matlab?

2 ビュー (過去 30 日間)
Swaprava Nath
Swaprava Nath 2011 年 9 月 2 日
I'm running a code which was working fine until I replaced some part of it with a recursive function. After introducing it, the same code with minimal change started giving segmentation fault error on every run. I tried debugging by instrumenting the code with breakpoints and some print statements, and I find that the trouble is not so much with the recursive function. The code is large, that is why I thought of not putting it here. But the rough structure is as follows:
while searchtime < MaxTime
state = computation of some parameters;
value = recursive(state);
compute some more stuff using the value;
searchtime = searchtime + 1;
end
I have used the diary option to save the terminal log, and I see that when it moves from the first searchtime to the next in the while loop, the segmentation fault occurs. Is it because the memory used by the recursive function does not get cleared automatically within two runs of the while loop? What is the workaround in such a case? I'm running Matlab 7.10.0 (R2010a). Thanks in advance
The segmentation fault comes from the code below:
for nodes = 1:n
if SearchEndTime(locationx(nodes),locationy(nodes))>0
Effort(nodes) = SearchEndTime(locationx(nodes),locationy(nodes)) - recruitedTime(nodes);
RegionEffort(locationx(nodes),locationy(nodes)) = RegionEffort(locationx(nodes),locationy(nodes)) + Effort(nodes);
if sum(RecruitmentTree(nodes,:)) == 0
isLeaf(nodes) = 1;
end
end
end
for nodes = 1:n
if RegionEffort(locationx(nodes),locationy(nodes))
rewardOfEachNode(nodes,1) = Effort(nodes)/RegionEffort(locationx(nodes),locationy(nodes));
rewardOfEachNode(nodes,2) = rewardOfEachNode(nodes,1) * PerFlagReward/2;
rewardOfEachNode(nodes,3) = rewardOfEachNode(nodes,1) * PerFlagReward/2;
end
end
set(0,'RecursionLimit',2000)
for rootnodes = k
recursiveRewardComputeDFS(rootnodes) ;
end
The function is as follows:
function [value] = recursiveRewardComputeDFS(currentnode)
global isLeaf;
global rewardOfEachNode;
global x;
global y;
global RecruitmentTree;
if isLeaf(currentnode) == 0
childrenofcurrentnode = find(RecruitmentTree(currentnode, :) == 1);
for child = childrenofcurrentnode
recursiveRewardComputeDFS(child);
rewardOfEachNode(currentnode,2) = rewardOfEachNode(currentnode,2) + rewardOfEachNode(child,2)/2;
rewardOfEachNode(currentnode,3) = rewardOfEachNode(currentnode,3) + exp(-dist(currentnode, child, x, y)) * rewardOfEachNode(child,3)/2;
end
end
end
  2 件のコメント
Swaprava Nath
Swaprava Nath 2011 年 9 月 2 日
A little addition: the segmentation fault seems to come from another recursion that appears below the above statement. I'm updating the question with the recursive function, since that is small.
Walter Roberson
Walter Roberson 2011 年 9 月 2 日
Increasing the recursion limit can cause unpredictable behavior if the memory for the recursion exceeds the stack size.

サインインしてコメントする。

回答 (2 件)

Walter Roberson
Walter Roberson 2011 年 9 月 2 日
To check: are you calling any mex routines (that you know of) ?
  1 件のコメント
Swaprava Nath
Swaprava Nath 2011 年 9 月 2 日
Nope, I'm not calling any C/C++ routine (as I understood about Mex routines, http://www.mathworks.com/support/tech-notes/1600/1605.html#intro). The called function is another matlab function file.

サインインしてコメントする。


Swaprava Nath
Swaprava Nath 2011 年 9 月 5 日
Well, I figured out the bug. The trouble was that the recursive function later was doing a depth first search, and the tree passed to that recursion wasn't a tree, rather it had a self loop. For a recursion that does a depth first search, falls into an infinite loop for the self loop, hence tries to open up infinite functions recursively and hence the segmentation fault. I fixed the tree to make it a valid one and it started working again.
  1 件のコメント
Walter Roberson
Walter Roberson 2011 年 9 月 5 日
http://www.catb.org/jargon/html/koans.html#id3141202

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeSoftware Development Tools についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by