Results 1 to 12 of 12
  1. #1
    0circle0 is offline Member
    Join Date
    Jul 2011
    Posts
    18
    Rep Power
    0

    Default 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:
    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();
    my destroyApp:
    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();
        }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

    Default

    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

  3. #3
    0circle0 is offline Member
    Join Date
    Jul 2011
    Posts
    18
    Rep Power
    0

    Default

    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;
    }

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

    Default

    awayCanvas.start();
    What class is awayCanvas? If not a JME class, then what JME class does it extend? [edit: Or what JME interface does it implement?]

    db

  5. #5
    0circle0 is offline Member
    Join Date
    Jul 2011
    Posts
    18
    Rep Power
    0

    Default

    The code is seperated into 2 files, AwayMidlet and AwayCanvas this is the code for AwayCanvas. A pastebin link is also supplied at bottom.

    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();
        }
    AwayCanvas pastebin, Code for AwayMidlet is supplied above.
    http://pastebin.com/6DVehSdW

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

    Default

    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;
      }
    }
    Java Code:
      public void destroyApp(boolean unconditional) {
        awayCanvas.stop();
    }
    There shouldn't be any need whatsoever to explicitly nullify the variables.

    db

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

    Default

    You had written
    not sure if there is a thread single thread. Not sure if they are trying to continue if I do have one.
    What do you think this is then?
    Java Code:
            Thread runner = new Thread(this);
            runner.start();
    Recommended reading:
    Lesson: Concurrency (The Java™ Tutorials > Essential Classes)

    And maybe
    Networking, User Experience, and Threads
    Using Threads in J2ME Applications

    db

  8. #8
    0circle0 is offline Member
    Join Date
    Jul 2011
    Posts
    18
    Rep Power
    0

    Default

    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
    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 (true) {
                try {
                    Thread.sleep(30);
                } catch (Exception e) {
                }
            }
        }
    
        public void start() {
            Thread runner = new Thread(this);
            runner.start();
        }
    }
    AwayMidlet
    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;
    }

  9. #9
    0circle0 is offline Member
    Join Date
    Jul 2011
    Posts
    18
    Rep Power
    0

    Default

    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.

    Java Code:
    public class MyThread implements Runnable {
        private boolean quit = false;
    
        public void run(){
    	while( !quit ){
    	    // do something
    	}
        }
    
        public void quit(){
    	quit = true;
        }
    }
    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().

    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:
    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;
        }
    Not sure what more it wants, as the small example has nothing but a shell and still causes the error.
    Last edited by 0circle0; 07-07-2011 at 04:05 PM.

  10. #10
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

    Default

    I already showed you that at #6

    db

  11. #11
    0circle0 is offline Member
    Join Date
    Jul 2011
    Posts
    18
    Rep Power
    0

    Default

    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 01:06 AM.

  12. #12
    0circle0 is offline Member
    Join Date
    Jul 2011
    Posts
    18
    Rep Power
    0

    Default

    It was still doing it cause of my while(true) loop in the pauseApp() function. with that removed it no longer does it. You have been a great help, will keep those links you posted bookmarked for future reference.
    Last edited by 0circle0; 07-08-2011 at 01:42 AM.

Similar Threads

  1. difference between destroyApp()
    By SIVAKUMAR.J in forum CLDC and MIDP
    Replies: 2
    Last Post: 07-26-2010, 07:39 AM
  2. destroyApp(boolean unconditional) method
    By thienphongvu in forum CLDC and MIDP
    Replies: 1
    Last Post: 08-07-2009, 10:14 AM
  3. [REQ] could anybody clean this method up for me?
    By harryblue in forum Advanced Java
    Replies: 5
    Last Post: 03-19-2009, 03:37 AM
  4. clean and Build
    By bhanu in forum Eclipse
    Replies: 3
    Last Post: 07-03-2008, 02:13 PM
  5. Clean the content of the JTextField
    By elizabeth in forum AWT / Swing
    Replies: 1
    Last Post: 07-26-2007, 09:38 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •