Results 1 to 12 of 12
Thread: What to clean up in destroyApp?
- 07-07-2011, 03:36 AM #1
Member
- Join Date
- Jul 2011
- Posts
- 18
- Rep Power
- 0
What to clean up in destroyApp?
What kind of things need to be cleaned up in destroyApp? I am quite new to java-me or java for that matter. Coming from C++ and Allegro. Some times, only after running the app I created, will stop sending or receiving calls/texts and every now and then will restart the phone when I close the app/game. Problem is solved by resetting phone. I assume it has something to do with the clean up procedure and my not doing it correctly.
variables:
my destroyApp:Java Code:public static class Character { private int x, y, frame, iframe; } public static Image block, blob, plants, trees, stumps; public static Sprite Block, Blob, Plants, Trees, Stumps; public int ScreenWidth = getWidth(), ScreenHeight = getHeight(); private static int MapOffsetX = 0, MapOffsetY = 0, BlockHeight = 32, BlockWidth = 32; private static int MapMaxY, MapMaxX; private static int SleepTime = 30, STEP = 4, PathAdd = 0; private static boolean ScrollLeft = false, ScrollRight = false, ScrollUp = false, ScrollDown = false; private static boolean MoveLeft = false, MoveRight = false, MoveUp = false, MoveDown = false, blobMove; private static boolean MoveMap = true; public static Character myBlob = new Character(); public static int myBlobFrameSeq[] = {0, 0, 1, 1, 2, 2}; public static Font font = null; public static Random rand = new Random(); private static final int MAPNumber = 0, MAPObjects = 1, MAPCollide = 2, MAPGValue = 3; private static final int MAPHValue = 4, MAPFValue = 5, MAPClosedList = 6, MAPParentList = 7; private static int Lowest = MapMaxY * MapMaxX + MapMaxX + 1; private static int NextToCheck = 0, FoundOpen = 0; private static int iy, ix, x, y, i, ox, oy, Count, Distance; private static int NoPath = 0; private static int EndX = 0, EndY = 0, StartX = 0, StartY = 0, Sx = 0, Sy = 0; private static int skipped = 0; private static int isOpen = 0; private static int[] toOpen = new int[2]; private static int o = 0; public static class FindMAP { private int[][][] map = new int[8][100][100]; private int[] OpenList = new int[1000]; private int[] mapXY = new int[2], Final = new int[2], XYFinal = new int[2]; private int[][] BlockStruct = new int[100][3]; } public static FindMAP tMap = new FindMAP();
Java Code:public void destroyApp(boolean unconditional) { AwayCanvas.block = null; AwayCanvas.blob = null; AwayCanvas.plants = null; AwayCanvas.trees = null; AwayCanvas.stumps = null; AwayCanvas.Block = null; AwayCanvas.Blob = null; AwayCanvas.Plants = null; AwayCanvas.Trees = null; AwayCanvas.Stumps = null; AwayCanvas.myBlob = null; AwayCanvas.myBlobFrameSeq = null; AwayCanvas.rand = null; AwayCanvas.tMap = null; awayCanvas = null; notifyDestroyed(); }
- 07-07-2011, 05:41 AM #2
Comments:
1. I've never found it necessary to nullify class fields in destroyApp(...). There has to be some other cause -- are you starting any Threads that might continue running after closing the application? Any network connections that aren't closed?
2. Never use class names that duplicate a class in the java.lang package and avoid using names already present in the Java ME API.
3. It's usually unwarranted to use so many static variables. I recommend you use instance fields instead.
4. Declare variables in the smallest possible enclosing scope. A variables which is assigned meaningful values in one method and used only by other methods called from that method, should be declared there and passed as a parameter.
5. To get better help sooner, post a SSCCE (Short, Self Contained, Compilable and Executable) example that demonstrates the problem.
db
- 07-07-2011, 08:07 AM #3
Member
- Join Date
- Jul 2011
- Posts
- 18
- Rep Power
- 0
1. No network connections, not sure if there is a thread single thread. Not sure if they are trying to continue if I do have one. Will post code at bottom.
2. Gotcha
3. I'll change that, just got carried away :)
4. Was thinking about that at work, not sure why I use variable types that are bigger then I need
5. Will do that as soon as I get off work.
Thanks.
Java Code:package Away; import javax.microedition.lcdui.Display; import javax.microedition.midlet.MIDlet; public class AwayMidlet extends MIDlet { public AwayMidlet() { awayCanvas = new AwayCanvas(); } public void startApp() { Display display = Display.getDisplay(this); awayCanvas.start(); display.setCurrent(awayCanvas); } public void pauseApp() { while(true) { try { Thread.sleep(30); } catch (Exception e) { } } } public void destroyApp(boolean unconditional) { AwayCanvas.block = null; AwayCanvas.blob = null; AwayCanvas.plants = null; AwayCanvas.trees = null; AwayCanvas.stumps = null; AwayCanvas.Block = null; AwayCanvas.Blob = null; AwayCanvas.Plants = null; AwayCanvas.Trees = null; AwayCanvas.Stumps = null; AwayCanvas.myBlob = null; AwayCanvas.myBlobFrameSeq = null; AwayCanvas.rand = null; AwayCanvas.tMap = null; awayCanvas = null; notifyDestroyed(); } private static AwayCanvas awayCanvas; }
- 07-07-2011, 08:24 AM #4
What class is awayCanvas? If not a JME class, then what JME class does it extend? [edit: Or what JME interface does it implement?]awayCanvas.start();
db
- 07-07-2011, 01:04 PM #5
Member
- Join Date
- Jul 2011
- Posts
- 18
- Rep Power
- 0
The code is seperated into 2 files, AwayMidlet and AwayCanvas this is the code for AwayCanvas. A pastebin link is also supplied at bottom.
AwayCanvas pastebin, Code for AwayMidlet is supplied above.Java Code:public AwayCanvas() { super(false); } public void run() { while (true) { updateScreen(getGraphics()); CheckChar(); MoveChar(tMap); MoveMap(tMap); try{Thread.sleep(SleepTime);} catch (Exception e){} } } public void start() { myBlob.x = 60; myBlob.y = 60; myBlob.frame = 0; myBlob.iframe = 1; MapMaxX = tMap.mapXY[0] = 100; MapMaxY = tMap.mapXY[1] = 100; for (y = 0; y <= 86; y++) for (x = 0; x <= 2; x++) if (x == 0) tMap.BlockStruct[y][x] = y; else if (x == 1) tMap.BlockStruct[y][x] = 0; else if (x == 2 && ((y >= 63 && y <= 84) || y == 57 || y == 58 || (y >= 47 && y <= 49) || (y >= 37 && y <= 39))) tMap.BlockStruct[y][x] = 1; SetupMap(); SetupFolliage(); int sx = myBlob.x / BlockWidth, sy = myBlob.y / BlockHeight, ex = rand.nextInt(97) + 1, ey = rand.nextInt(97) + 1; while (StartPathFinding(tMap, sx, sy, ex, ey) != 0){} Block = LoadS(block, "/maptiles.png", 32, 32, 1, 1, 0, null); Blob = LoadS(blob, "/blob.png", 32, 32, 16, 16, 1, myBlobFrameSeq); Plants = LoadS(plants, "/plants.png", 32, 32, 1, 1, 0, null); Stumps = LoadS(stumps, "/stumps.png", 64, 32, 1, 1, 0, null); Trees = LoadS(trees, "/trees.png", 64, 96, 1, 1, 0, null); Thread runner = new Thread(this); runner.start(); }
http://pastebin.com/6DVehSdW
- 07-07-2011, 01:39 PM #6
You need to stop that while(true) loop in destroyApp(...). One way to do this is
Java Code:public class AwayCanvas extends ... { // you didn't answer what class it extends private boolean keepRunning = true; public void run() { while (keepRunning) { : : } } public void stop() { keepRunning = false; } }There shouldn't be any need whatsoever to explicitly nullify the variables.Java Code:public void destroyApp(boolean unconditional) { awayCanvas.stop(); }
db
- 07-07-2011, 01:43 PM #7
You had written
What do you think this is then?not sure if there is a thread single thread. Not sure if they are trying to continue if I do have one.
Recommended reading:Java Code:Thread runner = new Thread(this); runner.start();
Lesson: Concurrency (The Java™ Tutorials > Essential Classes)
And maybe
Networking, User Experience, and Threads
Using Threads in J2ME Applications
db
- 07-07-2011, 01:45 PM #8
Member
- Join Date
- Jul 2011
- Posts
- 18
- Rep Power
- 0
I'll cut it down to the bare minimum, removing the need for any extra files. Testing each section, perhaps I'll find it. :)
This is the Bare minimum which is the template and still causes my phone to stop sending/getting texts and calls and eventually a hard reset on its own.
AwayCanvas
AwayMidletJava Code:package Away; import javax.microedition.lcdui.game.GameCanvas; public class AwayCanvas extends GameCanvas implements Runnable { public AwayCanvas() { super(false); } public void run() { while (true) { try { Thread.sleep(30); } catch (Exception e) { } } } public void start() { Thread runner = new Thread(this); runner.start(); } }
Java Code:package Away; import javax.microedition.lcdui.Display; import javax.microedition.midlet.MIDlet; public class AwayMidlet extends MIDlet { public AwayMidlet() { awayCanvas = new AwayCanvas(); } public void startApp() { Display display = Display.getDisplay(this); awayCanvas.start(); display.setCurrent(awayCanvas); } public void pauseApp() { while (true) { try { Thread.sleep(30); } catch (Exception e) { } } } public void destroyApp(boolean unconditional) { notifyDestroyed(); } private static AwayCanvas awayCanvas; }
- 07-07-2011, 01:54 PM #9
Member
- Join Date
- Jul 2011
- Posts
- 18
- Rep Power
- 0
Ok, I see what is going on. I have while(true) in the run() but no way to exit out of run other than a force quit by the device. Read through the first and third link you provided.
Edit: what I posted was not correct.
That is the code they have for stopping a thread, I do not understand how public void quit() is called, when to call it or if you even have to, but as the example is now it has no effect on my phone shutting off after I force quit. I do not have a way to quit though the application, only a force quit by termination of the application by the press of the power button to which the user is given a choice to either quit or not. Not sure how to intercept the request for termination and execute quit().Java Code:public class MyThread implements Runnable { private boolean quit = false; public void run(){ while( !quit ){ // do something } } public void quit(){ quit = true; } }
Not sure if it is the thread, as I manually call for the while loop to stop, at that point run is over. Not sure where it goes after that, from what I understand the system should automatically call destroyApp; however if this were the case it wouldn't just hang there waiting for me to hit the power button to force quit. How it was before and how it is now:
Not sure what more it wants, as the small example has nothing but a shell and still causes the error.Java Code:public void run() { while ( !quit ) { updateScreen(getGraphics()); CheckChar(); MoveChar(tMap); MoveMap(tMap); try{Thread.sleep(SleepTime);} catch (Exception e){} quit(); } } public void quit() { quit = true; }Last edited by 0circle0; 07-07-2011 at 03:05 PM.
- 07-07-2011, 02:48 PM #10
I already showed you that at #6
db
- 07-08-2011, 12:00 AM #11
Member
- Join Date
- Jul 2011
- Posts
- 18
- Rep Power
- 0
Ahh...Calling it there makes sense. Didn't see #6. This code, with the changes you have, still leads to same problem, no calls no texts and phone eventually shutting off. No other app does this, just the ones that I have made.
Java Code:package Away; import javax.microedition.midlet.MIDlet; public class AwayMidlet extends MIDlet { public AwayMidlet() { awayCanvas = new AwayCanvas(); } public void startApp() { awayCanvas.start(); } public void pauseApp() { while(true) { try { Thread.sleep(30); } catch (Exception e) { } } } public void destroyApp(boolean unconditional) { AwayCanvas.quit(); notifyDestroyed(); } private AwayCanvas awayCanvas; }Java Code:package Away; import javax.microedition.lcdui.game.GameCanvas; public class AwayCanvas extends GameCanvas implements Runnable { public AwayCanvas() { super(false); } public void run() { while (!quit) { try{Thread.sleep(30);} catch (Exception e){} } } public static void quit() { quit = true; } public void start() { Thread runner = new Thread(this); runner.start(); } private static boolean quit = false; }Last edited by 0circle0; 07-08-2011 at 12:06 AM.
- 07-08-2011, 12:13 AM #12
Member
- Join Date
- Jul 2011
- Posts
- 18
- Rep Power
- 0
Similar Threads
-
difference between destroyApp()
By SIVAKUMAR.J in forum CLDC and MIDPReplies: 2Last Post: 07-26-2010, 06:39 AM -
destroyApp(boolean unconditional) method
By thienphongvu in forum CLDC and MIDPReplies: 1Last Post: 08-07-2009, 09:14 AM -
[REQ] could anybody clean this method up for me?
By harryblue in forum Advanced JavaReplies: 5Last Post: 03-19-2009, 02:37 AM -
clean and Build
By bhanu in forum EclipseReplies: 3Last Post: 07-03-2008, 01:13 PM -
Clean the content of the JTextField
By elizabeth in forum AWT / SwingReplies: 1Last Post: 07-26-2007, 08:38 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks