Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (494)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Puzzled by bug and bug fix  (Read 1033 times)
0 Members and 1 Guest are viewing this topic.
Offline pin_kushn

Senior Newbie




Java games rock!


« Posted 2006-01-20 02:51:27 »

I bumped into a strange bug the other day.  I found a way to fix it, but I have no idea why it works and I'm wondering if someone can explain it.

Here is the situation.  I built a small set of classes to aid in my game development.  One class, "SoundFactory", has only static methods for loading and playing Clips.  It also has two static fields for HashMaps.  I never intended to create an instance of SoundFactory.

The first game that I wrote with the help of SoundFactory worked fine, but the next one did not.  The code simply stopped executing somewhere soon after a call to a SoundFactory static method.  Subsequent Game runs always stopped at the same place, until I commented out a few miscellaneous instantiations.  Then the code consistently stopped at a different spot.  The "stopping spots" always included an instantiation.  No errors were thrown.  Code just stopped executing.

So why did Game1 work and not Game2?  I commented out code until I finally found the difference.  Game1 had the following field declaration/assignment:

Color color = Color.BLUE;

When I added this seemingly irrelevant line as a field for Game2, the code ran fine.

Can anyone explain what I was doing wrong?  I am guessing that I should abandon the whole static idea, but I am still curious about what the hang-up might be.

I code in 1.4 using Eclipse with my Mac, 10.3.

thanks,

josh

Offline bauerr

Junior Member




Java!!!!!!!!!!! !!!


« Reply #1 - Posted 2006-01-20 06:57:56 »

You should post (parts of) your code to get proper help.
Offline pin_kushn

Senior Newbie




Java games rock!


« Reply #2 - Posted 2006-01-20 21:17:47 »

Ok, here is a pared down version of Game and SoundFactory.  The code hangs in the Game constructor on creating a JFrame.  The same hang happened with all sorts of other classes -- it doesn't have anything to do with JFrame.  If I do not call the SoundFactory loadClip method, the JFrame is created.  Most puzzling is this: when I leave in the call to loadClip, but also create a reference to a static constant, everything works fine!??!?  This only works if a actually assign the field to a static constant: just declaring it doesn't work.

Any insights would be appreciated.

Quote
public class Game {
   
   //Color x = Color.BLACK;
   
   public Game() {
      File b = new File("resources/Boom.wav");
      SoundFactory.loadClip(b, 3);

      System.out.println("About to create JFrame");
      JFrame jf = new JFrame();
      //following line doesn't happen unless I uncomment Color, above
      System.out.println("Created JFrame");
   }

   public static void main (String [] args) {
      new Game ();
   }
}

Quote
public class SoundFactory {

   private static HashMap availableClips;
   
   static {
      availableClips = new HashMap();
   }

   public static void loadClip (File soundFile, int howManyCopies) {
      try {
         for (int x = 0; x < howManyCopies; x++) {
            InputStream is = new FileInputStream(soundFile);
            loadClip (is, soundFile.getName());
            is.close();
         }
      }
      catch (Exception e) {
         e.printStackTrace();
      }
   }
   
   public static void loadClip (InputStream is, String name) {

      if (!availableClips.containsKey(name)) {
         availableClips.put(name, new LinkedList());
      }
      
      List list = (List) availableClips.get(name);

      AudioInputStream audioInputStream = null;
      
      try {
         audioInputStream = AudioSystem.getAudioInputStream(is);
      } catch (UnsupportedAudioFileException e1) {
         e1.printStackTrace();
      } catch (IOException e1) {
         e1.printStackTrace();
      }
      
      AudioFormat   format = audioInputStream.getFormat();
      DataLine.Info info = new DataLine.Info(Clip.class, format);
      Clip clip = null;
      try {
         clip = (Clip) AudioSystem.getLine(info);
         clip.open(audioInputStream);
         list.add(clip);
         
         audioInputStream.close();
      }
      catch (LineUnavailableException e) {
         e.printStackTrace();
      }
      catch (IOException e) {
         e.printStackTrace();
      }
   }
}
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Jeff

JGO Coder




Got any cats?


« Reply #3 - Posted 2006-01-22 18:59:58 »

This really sounds like a race condition as the assignmetn has no effect on the code in  question other then chnaging the timing.

Id have to look closer though to determine exactly what is racing...

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Offline pin_kushn

Senior Newbie




Java games rock!


« Reply #4 - Posted 2006-01-24 14:56:09 »

what is a race condition?
Offline Mr_Light

Senior Member


Medals: 1


shiny.


« Reply #5 - Posted 2006-01-24 15:47:18 »

http://java.sun.com/docs/books/tutorial/essential/threads/priority.html its been a while since I read it but I beleave it was in that section, its somewhere in the trail adleast

It's harder to read code than to write it. - it's even harder to write readable code.

The gospel of brother Riven: "The guarantee that all bugs are in *your* code is worth gold." Amen brother a-m-e-n.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Dwinin (19 views)
2014-09-12 09:08:26

Norakomi (54 views)
2014-09-10 13:57:51

TehJavaDev (63 views)
2014-09-10 06:39:09

Tekkerue (31 views)
2014-09-09 02:24:56

mitcheeb (53 views)
2014-09-08 06:06:29

BurntPizza (37 views)
2014-09-07 01:13:42

Longarmx (23 views)
2014-09-07 01:12:14

Longarmx (27 views)
2014-09-07 01:11:22

Longarmx (27 views)
2014-09-07 01:10:19

mitcheeb (35 views)
2014-09-04 23:08:59
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!