2

I have a simple m-file

function [fRate,Height,Width] = media(filename)  
obj = mmreader(filename);  
fRate = obj.FrameRate;  
Width = obj.Width;  
Height = obj.Height;  
end

Which I have successfully compiled using MATLAB Builder JA into a .jar file.

I have tested the .jar file in a single threaded application and it work with no problem.

The error came when I use it in a multi threaded GUI application. I run the .jar as a thread in one of the class I created and the following error occur.

An unexpected error has been detected by Java Runtime Environment:

EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x6d9c08b0, pid=5920, tid=4788

Java VM: Java HotSpot(TM) Client VM (10.0-b19 mixed mode windows-x86) Problematic frame:

C [jvm.dll+0x1108b0]

After debugging, I found that the error occur when my thread is calling
media = new Media(); (I name my .jar as Media.jar)

This is my Java code:
// mediaProperty.java

public class mediaProperty implements Runnable {

public void mediaProperty() {

    Matlab_options matlab = new Matlab_options();
    Object[] mediaProp = null;
    java.util.List lstMedia = new ArrayList();
    Media media = null;

    try {

        media = new Media();
        ...

        mediaProp = media.media(3, lstMedia);

        ...
    } catch (Exception p) {
        System.out.println("Exception: " + p.toString());
    } finally {
        MWArray.disposeArray(mediaProp);
        if (media != null) {
            media.dispose();
        }
    }
}

public void run() {
    mediaProperty();
}

}

// GUI.java

private Thread mediap;

if (mediap == null) {
mediap = new Thread(new mediaProperty());
mediap.start();
}

What is wrong? Is it my code?

HH.
  • 769
  • 2
  • 10
  • 21
  • I've just added my java code. From what I see I have already created a dedicated thread for the .jar but the error alway occur when it try to init the constructor media = new Media(); – HH. Jan 04 '10 at 13:12
  • 1
    Adam is right, refer to the following website for detail: http://www.mathworks.com/support/solutions/en/data/1-3TIHU3/index.html?product=MJ&solution=1-3TIHU3 So I need to create a 2nd process not thread as the threads I created in my app still share the same process. – HH. Jan 05 '10 at 05:19

1 Answers1

3

My guess is that MATLAB requires you to access it from exactly one thread. You say it works in a single threaded application, perhaps you need to start a dedicated thread for interacting with MATLAB to get this to work correctly.

See also
Thread safety of Matlab engine API

Community
  • 1
  • 1
Adam Goode
  • 7,380
  • 3
  • 29
  • 33
  • If this is the case, How do I start a dedicated thread in java? – HH. Jan 04 '10 at 05:32
  • Well, it depends on the specifics of how you interface with MATLAB. Can you give an example of the Java code you use to call your jar? You probably want to create a thread before you call into your jar at all. – Adam Goode Jan 04 '10 at 05:41
  • A good book for this is Java Concurrency in Practice. Or look at http://java.sun.com/javase/6/docs/api/java/util/concurrent/package-summary.html – Adam Goode Jan 04 '10 at 05:43