Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

#7 memory management problem

CLIPSJNI
closed
nobody
Crash (2)
5
2014-10-30
2010-02-01
No

Hello people,
I found out that there is a serious issue in CLIPSJNI that prevent it to be used in real-word applications.
There is a memory leak or other memory management issue when creating or freeing CLIPS environments.

I have tested in both MacOS X and Linux.

Testing environment:

A) Mac
MacOS X: 10.5.8
java version "1.5.0_16"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_16-b06-284)
Java HotSpot(TM) Client VM (build 1.5.0_16-133, mixed mode, sharing)

B) Linux
Linux cflex-assets 2.6.24-16-generic #1 SMP Thu Apr 10 13:23:42 UTC 2008 i686 GNU/Linux
java version "1.5.0_22"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_22-b03)
Java HotSpot(TM) Server VM (build 1.5.0_22-b03, mixed mode)

How to reproduce:

Just run the following java program:

import junit.framework.TestCase;
import CLIPSJNI.Environment;
import CLIPSJNI.PrimitiveValue;

public class ClipsTest extends TestCase{

public void testClipsEnvironment\(\) \{

    for \(int j=0; j<100; j++\) \{
        //Create an environment
        System.out.println\("Iteraction: "+j\);
        Environment clips = new Environment\(\);
        for \(int i=0; i<100; i++\) \{
            //Do something very simple... Just to use the enviroment
            clips.eval\("\(assert \(dummy-fact "+i+"\)\)"\);
            String evalStr = "\(find-all-facts \(\(?f dummy-fact\)\) TRUE\)";
            PrimitiveValue pv = clips.eval\(evalStr\);
            if \(pv==null\)
                fail\("ask-question-fact not found"\);
            if \(i%10 == 0\)
                clips.reset\(\);
        \}
    \}      
\}

}

Results:

A) MAC
Iteraction: 0
Iteraction: 1
Iteraction: 2
Iteraction: 3
Iteraction: 4
Iteraction: 5
Iteraction: 6
Iteraction: 7
Iteraction: 8
Iteraction: 9
Iteraction: 10
Iteraction: 11
Iteraction: 12
Iteraction: 13
Iteraction: 14
Iteraction: 15
Iteraction: 16
Iteraction: 17
Iteraction: 18
Iteraction: 19
Iteraction: 20
Iteraction: 21
Iteraction: 22
Iteraction: 23
Iteraction: 24
Iteraction: 25
Iteraction: 26
Iteraction: 27
Iteraction: 28
Iteraction: 29
Iteraction: 30
Iteraction: 31
Iteraction: 32
Iteraction: 33
Iteraction: 34
Iteraction: 35
Iteraction: 36
Iteraction: 37
Iteraction: 38
Iteraction: 39
Iteraction: 40
Iteraction: 41
java(17307,0x815e00) malloc: *** error for object 0x131650: incorrect checksum for freed object - object was probably modified after being freed.
*** set a breakpoint in malloc_error_break to debug
Invalid memory access of location 004c5100 eip=9722bc58

B) Linux
Iteraction: 0
Iteraction: 1
Iteraction: 2
Iteraction: 3
Iteraction: 4
Iteraction: 5
Iteraction: 6
Iteraction: 7
Iteraction: 8
Iteraction: 9
Iteraction: 10
Iteraction: 11
Iteraction: 12
Iteraction: 13
Iteraction: 14
Iteraction: 15
Iteraction: 16
Iteraction: 17
Iteraction: 18
Iteraction: 19
Iteraction: 20
Iteraction: 21
Iteraction: 22
Iteraction: 23
Iteraction: 24
Iteraction: 25
Iteraction: 26
Iteraction: 27
Iteraction: 28
Iteraction: 29
Iteraction: 30
Iteraction: 31
Iteraction: 32
Iteraction: 33
Iteraction: 34
Iteraction: 35
Iteraction: 36
Iteraction: 37
Iteraction: 38
Iteraction: 39
Iteraction: 40
Iteraction: 41
Iteraction: 42
Iteraction: 43
Iteraction: 44
Iteraction: 45
Iteraction: 46
Iteraction: 47
Iteraction: 48
Iteraction: 49
Iteraction: 50
Iteraction: 51
Iteraction: 52
Iteraction: 53
Iteraction: 54
Iteraction: 55
Iteraction: 56
#
# An unexpected error has been detected by Java Runtime Environment:
#
# SIGSEGV (0xb) at pc=0xb7e9ef88, pid=12517, tid=3084970896
#
# Java VM: Java HotSpot(TM) Server VM (11.0-b16 mixed mode linux-x86)
# Problematic frame:
# C [libc.so.6+0x69f88]
#

Tks,
Rodrigo.

Discussion

  • thomas gentsch
    thomas gentsch
    2010-06-18

    What happens if you do the same in pure C code?
    1. build c lib with debug enabled
    2. Create a small C prog which does the same as your java code
    3. on linux, run:
    valgrind testprog

    Valgrind is really good about checking memory access problems etc and this way it may reveal
    - whether it is a problem in the JNI layer or already in pure C code
    - indicate where the problem is

     
  • Gary Riley
    Gary Riley
    2011-03-05

    Fixed in the CLIPSJNI 0.3 release. The java classes were set up to release the underlying C data structures when the finalize method was called for the Environment class, but that runs in a separate thread from the JNI code and so won't work. You now must explicitly deallocate the C data structures from the Java code when you no longer want to use the environment. From the comments in the example programs:

    This example creates just a single environment. If you create multiple environments,call the destroy method when you no longer need the environment. This will free the C data structures associated with the environment.

    clips = new Environment();
    .
    .
    .
    clips.destroy();

    Calling the clear, reset, load, loadFacts, run, eval, build, assertString,
    and makeInstance methods can trigger CLIPS garbage collection. If you need
    to retain access to a PrimitiveValue returned by a prior eval, assertString,
    or makeInstance call, retain it and then release it after the call is made.

    PrimitiveValue pv1 = clips.eval("(myFunction foo)");
    pv1.retain();
    PrimitiveValue pv2 = clips.eval("(myFunction bar)");
    .
    .
    .
    pv1.release();

     
  • Gary Riley
    Gary Riley
    2011-03-05

    • status: open --> closed-fixed