MATLAB Answers

What is the reason of Segmentation Fault in matlab?

46 views (last 30 days)
Swaprava Nath
Swaprava Nath on 2 Sep 2011
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 Comments
Walter Roberson
Walter Roberson on 2 Sep 2011
Increasing the recursion limit can cause unpredictable behavior if the memory for the recursion exceeds the stack size.

Sign in to comment.

Answers (2)

Walter Roberson
Walter Roberson on 2 Sep 2011
To check: are you calling any mex routines (that you know of) ?
  1 Comment
Swaprava Nath
Swaprava Nath on 2 Sep 2011
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.

Sign in to comment.


Swaprava Nath
Swaprava Nath on 5 Sep 2011
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 Comment
Walter Roberson
Walter Roberson on 5 Sep 2011
http://www.catb.org/jargon/html/koans.html#id3141202

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!