Results 1 to 3 of 3
  1. #1
    Rolf is offline Member
    Join Date
    Jan 2011
    Posts
    2
    Rep Power
    0

    Default Member variable corruption

    Hi all,

    I have a very odd multi-threading problem that I've run out of ideas with. If anybody has any suggestions they'd be greatly appreciated.

    Here's the structure, bottom up:

    I have a class (we'll call it Engine) with some private member variables, and some local member variables in the methods.

    I have another class that is Runnable, which we'll call SingleEngine. The run method instantiates Engine and calls a method within it, which will loop until flagged to exit.

    A third class, called EngineController, instantiates 2 SingleEngines and starts them as threads.

    So EngineController has 2 instances of SingleEngine, each being a thread, and each SingleEngine instantiates an Engine.

    Now what I'm finding is, the class member variables in SingleEngine are somehow becoming equal between the two instances (and threads). I know I'm not looking at the same Engine object, because the local method variables are different. But if I change a class member variable in one instance of engine, it changes in the other, and netbeans tells me the variables are in fact pointing at exactly the same memory.

    Neither of the two engines interact with each other. And some of the member variables are only accessed from within an Engine class. All of the class variables are private.

    Just for an added twist, the application actually has more than 2 just of these SingleEngine threads running, yet the problem only happens between 2 of them (not consistent which 2).

    I hope that makes sense. Does anybody have any ideas what to look at/what could be causing this? Feel free to ask for specific details, I'm trying to limit it to the basic description of what I have.

    Many thanks,
    Rolf

  2. #2
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    Is anything static? If so, that might be your problem. Is there a way you could strip the code down to the bare mimimum essential to reproduce the problem, and then post it here?

  3. #3
    Rolf is offline Member
    Join Date
    Jan 2011
    Posts
    2
    Rep Power
    0

    Default

    Hi Toadally,

    I think I might have fixed it, though I still don't understand what was/is going off.

    There's no static variables involved.

    It turns out that each instance of SingleEngine was somehow pointing to the same Engine instance (not initially, this happened through weird magic at runtime). I hadn't detected this originally because the local method variables in each instance of Engine were different, but i guess this is actually something to do with the way the stack is organised. I spotted in netbeans that if i trace up the call stack to each instance of SingleEngine, their associated engine variables both had the same memory reference. So that made the situation slightly less weird, but no solutions here.

    What i changed to fix it was.... In SingleEngine, the variable 'engine' was only instantiated within Run(), not within the contructor. I moved it to the constructor, and somehow, that fixed the problem (or appears to have).

    This still makes absolutely no sense to me. the 'engine' variable was private, and the only methods within SingleEngine that ever touch it are run (instantiates engine), resume (calls engine.resume()), and terminate (calls engine.resume()). Those are the only 3 methods in the class! Yet by instantiating engine in run(), somehow, the engine reference could get randomly changed to that of another thread.

    I've run the program some 300+ times since moving it to the constructor, and the bug appears to have been resolved. I can only assume it's something to do with the optimisation of thread calls, but I still don't "see it".

    Fingers crossed that I have actually fixed it (after 3 days of tearing my hair out)....

Similar Threads

  1. New member looking for help
    By Bas76 in forum New To Java
    Replies: 14
    Last Post: 04-13-2011, 12:37 PM
  2. Session Variable corruption example..
    By joe131 in forum Advanced Java
    Replies: 1
    Last Post: 05-10-2010, 03:51 PM
  3. New member
    By curt@presidential.com in forum Introductions
    Replies: 2
    Last Post: 12-18-2008, 02:36 PM
  4. New Member
    By Laura Warren in forum Introductions
    Replies: 9
    Last Post: 11-22-2008, 02:39 PM
  5. new member-need help
    By vandana in forum New To Java
    Replies: 2
    Last Post: 01-21-2008, 07:01 AM

Tags for this Thread

Posting Permissions

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