You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@mina.apache.org by Rajeshwari M <ra...@gmail.com> on 2008/11/06 07:24:13 UTC

Byte Buffer release and acquire doubts

Hi All,
          I've written small program to test bytebuffer acquire and release
methods.

Program:

import org.apache.mina.common.ByteBuffer;

public class ByteBufferTest {

    public static void test() {
        try{
            ByteBuffer b = ByteBuffer.allocate(4000);
            byte b1=1;
            b.put(b1);
            b.acquire();
            b.release();
            b.release(); // If I call release() method one more time. I get
IlegalStateException in next line of acquire(). If I dont call release()
second time. acquire() is not throwing any error.
            b.acquire();

        }catch(Exception e){
            e.printStackTrace();
        }

    }

    public static void main(String a[]){
        test();
    }
}

Does that means, we need to call release() two times to release the buffer
actually ???

Re: Byte Buffer release and acquire doubts

Posted by Emmanuel Lecharny <el...@gmail.com>.
Rajeshwari M wrote:
> Hi All,
>   
Hi,

>           I've written small program to test bytebuffer acquire and release
> methods.
>
> Program:
>
> import org.apache.mina.common.ByteBuffer;
>
> public class ByteBufferTest {
>
>     public static void test() {
>         try{
>             ByteBuffer b = ByteBuffer.allocate(4000);
>             byte b1=1;
>             b.put(b1);
>             b.acquire();
>             b.release();
>             b.release(); // If I call release() method one more time. I get
> IlegalStateException in next line of acquire(). If I dont call release()
> second time. acquire() is not throwing any error.
>             b.acquire();
>
>         }catch(Exception e){
>             e.printStackTrace();
>         }
>
>     }
>
>     public static void main(String a[]){
>         test();
>     }
> }
>
> Does that means, we need to call release() two times to release the buffer
> actually ???
>   
Well, the best would be to look at the current code, and try to see if 
it's the case. Either you do it, or we do it, but the solution will be 
to look at the code anyway, and see if there is a bug or not :)


-- 
--
cordialement, regards,
Emmanuel Lécharny
www.iktek.com
directory.apache.org



Re: Byte Buffer release and acquire doubts

Posted by Ashish <pa...@gmail.com>.
Nope you don't need to call release twice to free it.

>From basic analysis there seems to be a minor bug (may be) in the implementation

Essentially, the release() function checks for refCount (Line 447) and
checks for less than equal to 0. If the condition is true
it throws an IllegalStateException. The default refCount is 1, so once
you call acquire(), it becomes 2, first release makes it 1, now call
second release(), condition at line 447 becomes false, so illegal
state exception is not thrown as value of refCount is 1 :-),
after that, refCount-- (Line 453) make it 0. So next time you call
acquire(), it checks again for the refCount as if (refCount <= 0), and
which come to be true, and exception is thrown :-)

MINA Experts can comment more on my interpretation.

In Sumary,

you have to call release() as many times as you called acquire()

Happy Coding :-)


On Thu, Nov 6, 2008 at 11:54 AM, Rajeshwari M <ra...@gmail.com> wrote:
> Hi All,
>          I've written small program to test bytebuffer acquire and release

> }
>
> Does that means, we need to call release() two times to release the buffer
> actually ???
>



-- 
thanks
ashish

Blog: http://www.ashishpaliwal.com/blog

My Photo Galleries: http://www.pbase.com/ashishpaliwal