allocate memory problem

Hello. I have created an applet and i put it to my java card .

Iso, can write to an array but when I repeat the procedure for 15 times(after 16 ) I get an error and the status word that it returns is "6F00",

. I read somewhere that i have to allocate the memory in constructor because if i dont the memory is never free.

can you please tell me what i have to do?

my code is:

package com.acme.options;

import javacard.framework.*;

public class optionsApplet extends Applet {

final static byte Helloword_CLA = (byte) 0xB0;

final static byte WR_HELLO = (byte) 0x57;

private short i = 0;

private short a = 0;

private short x = 0;

byte[] hello=new byte[0];

byte[] hello2;

byte[] hello3;

public static void install(byte[] bArray, short bOffset, byte bLength) {

// GP-compliant JavaCard applet registration

new optionsApplet().register(bArray, (short) (bOffset + 1),

bArray[bOffset]);

}

public void process(APDU apdu) {

// Good practice: Return 9000 on SELECT

if (selectingApplet()) {

return;

}

byte[] buffer = apdu.getBuffer();

if (buffer[ISO7816.OFFSET_CLA] != Helloword_CLA)

ISOException.throwIt(ISO7816.SW_CLA_NOT_SUPPORTED);

switch (buffer[ISO7816.OFFSET_INS]) {

case WR_HELLO:

wr_hello(apdu);

return;

case RESET:

default:

}

}

private void wr_hello(APDU apdu) throws APDUException {

byte[] temp = apdu.getBuffer();

short data_length = (short) (temp[ISO7816.OFFSET_LC] & 0xFF);

hello2 = new byte[data_length];

short read_count = apdu.setIncomingAndReceive();

Util.arrayCopy(temp, ISO7816.OFFSET_CDATA, hello2, (short) 0,

(short) data_length);

hello3 = hello;

hello = new byte[hello3.length + hello2.length];

for (i = 0; i < hello3.length; ++i) {

hello = hello3;

}

for (i = (short) hello3.length; i < hello.length; ++i) {

hello = hello2[x];

x++;

}

i = 0;

a = 0;

x = 0;

}

}

[2151 byte] By [flasholinosa] at [2007-11-27 10:12:46]
# 1

Just to make it readable to the human eye:

package com.acme.options;

import javacard.framework.*;

public class optionsApplet extends Applet {

final static byte Helloword_CLA = (byte) 0xB0;

final static byte WR_HELLO = (byte) 0x57;

private short i = 0;

private short a = 0;

private short x = 0;

byte[] hello=new byte[0];

byte[] hello2;

byte[] hello3;

public static void install(byte[] bArray, short bOffset, byte bLength) {

// GP-compliant JavaCard applet registration

new optionsApplet().register(bArray, (short) (bOffset + 1),

bArray[bOffset]);

}

public void process(APDU apdu) {

// Good practice: Return 9000 on SELECT

if (selectingApplet()) {

return;

}

byte[] buffer = apdu.getBuffer();

if (buffer[ISO7816.OFFSET_CLA] != Helloword_CLA)

ISOException.throwIt(ISO7816.SW_CLA_NOT_SUPPORTED);

switch (buffer[ISO7816.OFFSET_INS]) {

case WR_HELLO:

wr_hello(apdu);

return;

case RESET:

default:

}

}

private void wr_hello(APDU apdu) throws APDUException {

byte[] temp = apdu.getBuffer();

short data_length = (short) (temp[ISO7816.OFFSET_LC] & 0xFF);

hello2 = new byte[data_length];

short read_count = apdu.setIncomingAndReceive();

Util.arrayCopy(temp, ISO7816.OFFSET_CDATA, hello2, (short) 0,

(short) data_length);

hello3 = hello;

hello = new byte[hello3.length + hello2.length];

for (i = 0; i < hello3.length; ++i) {

hello = hello3;

}

for (i = (short) hello3.length; i < hello.length; ++i) {

hello = hello2[x];

x++;

}

i = 0;

a = 0;

x = 0;

}

}

Lillesanda at 2007-7-28 15:22:29 > top of Java-index,Java Mobility Forums,Consumer and Commerce...
# 2

Okay, the problem is the line

hello = new byte[hello3.length + hello2.length];

Every time you execute that line hello3.length +hello2.length bytes of memory is allocated. In general, you should be very careful about when you use the "new" variable.

That should answer your question, but apart from that I have no idea what your program is supposed to do, so I can't give you any more help.

Lillesanda at 2007-7-28 15:22:30 > top of Java-index,Java Mobility Forums,Consumer and Commerce...
# 3

thanks for your reply!!

I know that my problem is

hello = new byte[hello3.length + hello2.length];

but i don'w know how to avoid it.

All i want to do is to write many data to the variable hello is there another way or something?

flasholinosa at 2007-7-28 15:22:30 > top of Java-index,Java Mobility Forums,Consumer and Commerce...
# 4

You can try to circumvent the problem of memory leakage. The garbage collector is optional in Java Card. So if you have an implementation which supports garbage collection just add the following lines at the end your your method:if (JCSystem.isObjectDeletionSupported()) {

JCSystem.requestObjectDeletion();

}

This will garbage collect at an undefined time your unreferenced by array(s).

lexdabeara at 2007-7-28 15:22:30 > top of Java-index,Java Mobility Forums,Consumer and Commerce...
# 5

thank you my friend :)

flasholinosa at 2007-7-28 15:22:30 > top of Java-index,Java Mobility Forums,Consumer and Commerce...