5
votes

I was stumped by a segmentation fault in MATLAB. It seems like it was caused by an anonymous function that was loaded from a mat file. The original anonymous function handle was:

@(x)scaledNlfun(x,@logexp1,1e3) 

But when it is loaded, it becomes:

@sf%1@(x)scaledNlfun(x,@logexp1,1e3)

It seems to be okay, when I call it in command line, but it creates a segmentation fault (or Segmentation violation) within a function. Not the function call itself, but a few lines after that. In debugging mode, if I step through the statement, it is fine as well.

The stack trace shows bunch of

[  0] 0x00002b20b97baba4 /usr/local/MATLAB/R2013a/bin/glnxa64/libmwm_interpreter.so+04127652

and it happens on both MATLAB 2012a and 2013a on a Linux 2.6.18-371.3.1.el5 SMP.

This function handle was saved within a parfor loop using '-v7.3' option because the struct that contains the handle was too big. If I replace the anonymous function after loading the mat file, everything works fine, so I'm thinking the matlab load function has a bug.

Unfortunately, I cannot create a minimal example to reproduce the error. I tried saving anonymous function handles within parfor with '-v7.3', but without the other complex data structures, it seems to work fine. But I have 80 mat files that would reliably crash matlab (many of them more than 1GB).

In any case, does anybody know what that "@sf%" mean? (it's not the stateflow toolbox)

1
Does the implementation of scaledNlfun have any effect on the segv? - maxywb
@maxywb I haven't tested that. It takes few hours to run the code that saves the mat file, so it's been difficult to test things, unfortunately. - Memming
Can you run functions on it? - horchler
And how did you save the the anonymous function? With str2func? Did you use the '-struct' option for `save? - horchler
Not sure if I understand correctly, but normally I would save the function handle before any parfor loop if possible. I tried to give some general tips, but without actual code that causes the problem it is a bit like shooting with closed eyes. - Dennis Jaheruddin

1 Answers

0
votes

The core of the problem seems to be that you have @sf%1@ where you would expect @ just looking at this, I can think of a few possibilities:

  1. Somehow sf%1@ was inserted after the original @
  2. Somehow @sf%1@ was substituted in place of the original @
  3. Somehow @sf%1 was attached before the original @

I would actually bet on the third one, but here are the most logical scenarios I can think of that could cause this problem:

  1. Perhaps there was an invisible char?
  2. Perhaps some kind of strange character conversion?
  3. Probably a situation where two things are stored in a variable instead of one. Perhaps something like @s or @sf and some separating characters.

All in all, this does not explain why it would go well if you run the entire program in the console, but perhaps you just ran part of it. In that case these could be some things to look out for.