Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Global Variable..(Simple but difficult....)

Global Variable..(Simple but difficult....)

Scheduled Pinned Locked Moved C / C++ / MFC
c++helpannouncementcomdebugging
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sumit Kapoor
    wrote on last edited by
    #1

    Hi.. I'm using Global variable in VC++ MFC by declaring variable in .cpp file. then I've to use "extern" to acces that variable from other file..that's OK!..if I'm working normal..But when I use to access that Variable from thread..then It gives Error in Relase varsion(NOTE: not giving error in DEBUG version).. 1. Whenever I need to write new value I use CCriticalSection variable & Lock & unlock while operation of writing.. e.g: CCriticalSection a1; a1.Lock(); bStopRightDownThread=true; a1.Unlock(); 2. This value is Continue used by thread & checked by thread..if this value change then thread take action as per value... Problem : It is giving Illigal Operation error & close application in case of RELEASE version, but working fine in case of DEBUG version. WHAT DO YOU THINK REASON OF THIS? THanks..I'm waiting for your reply...:) ---Sumit Kapoor--- sumit_kapoor1980@hotmail.com

    V R 2 Replies Last reply
    0
    • S Sumit Kapoor

      Hi.. I'm using Global variable in VC++ MFC by declaring variable in .cpp file. then I've to use "extern" to acces that variable from other file..that's OK!..if I'm working normal..But when I use to access that Variable from thread..then It gives Error in Relase varsion(NOTE: not giving error in DEBUG version).. 1. Whenever I need to write new value I use CCriticalSection variable & Lock & unlock while operation of writing.. e.g: CCriticalSection a1; a1.Lock(); bStopRightDownThread=true; a1.Unlock(); 2. This value is Continue used by thread & checked by thread..if this value change then thread take action as per value... Problem : It is giving Illigal Operation error & close application in case of RELEASE version, but working fine in case of DEBUG version. WHAT DO YOU THINK REASON OF THIS? THanks..I'm waiting for your reply...:) ---Sumit Kapoor--- sumit_kapoor1980@hotmail.com

      V Offline
      V Offline
      V 0
      wrote on last edited by
      #2

      You can try this: From thread post/send message which calls a function from your class which will set your variable. I once had something similar and we thought the reason was that a thread works on a different part of memory. (but I'm not sure) Hope this helps. "If I don't see you in this world, I'll see you in the next one... and don't be late." ~ Jimi Hendrix

      1 Reply Last reply
      0
      • S Sumit Kapoor

        Hi.. I'm using Global variable in VC++ MFC by declaring variable in .cpp file. then I've to use "extern" to acces that variable from other file..that's OK!..if I'm working normal..But when I use to access that Variable from thread..then It gives Error in Relase varsion(NOTE: not giving error in DEBUG version).. 1. Whenever I need to write new value I use CCriticalSection variable & Lock & unlock while operation of writing.. e.g: CCriticalSection a1; a1.Lock(); bStopRightDownThread=true; a1.Unlock(); 2. This value is Continue used by thread & checked by thread..if this value change then thread take action as per value... Problem : It is giving Illigal Operation error & close application in case of RELEASE version, but working fine in case of DEBUG version. WHAT DO YOU THINK REASON OF THIS? THanks..I'm waiting for your reply...:) ---Sumit Kapoor--- sumit_kapoor1980@hotmail.com

        R Offline
        R Offline
        Ryan Binns
        wrote on last edited by
        #3

        Your code for using a critical section is flawed. What you have there won't do anything. The critical section object must be shared just like the boolean you're trying to modify:

        CCriticalSection _bStopRightDownThread;
        bool bStopRightDownThread;
        ...
        _bStopRightDownThread.Lock();
        bStopRightDownThread = true;
        _bStopRightDownThread.Unlock();

        The fact that it produces an error in the release version but not in the debug version is (in my experience) totally coincidental.

        Ryan

        "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

        S 1 Reply Last reply
        0
        • R Ryan Binns

          Your code for using a critical section is flawed. What you have there won't do anything. The critical section object must be shared just like the boolean you're trying to modify:

          CCriticalSection _bStopRightDownThread;
          bool bStopRightDownThread;
          ...
          _bStopRightDownThread.Lock();
          bStopRightDownThread = true;
          _bStopRightDownThread.Unlock();

          The fact that it produces an error in the release version but not in the debug version is (in my experience) totally coincidental.

          Ryan

          "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

          S Offline
          S Offline
          Sumit Kapoor
          wrote on last edited by
          #4

          Ya..Dear, Ya Dear your are right, I'm facing problem in release version,not in Debug. But can you give more clear idea..how to do? // Take these as Global CCriticalSection _bStopRightDownThread; bool bStopRightDownThread; ..... ..... ..... // In particular function use as follow _bStopRightDownThread.Lock(); bStopRightDownThread = true; _bStopRightDownThread.Unlock(); Do Your means this? If yes..then I'm using this in many files, DO u means I've to share Critical section vaiable to all using: extern CCriticalSection _bStopRightDownThread; or induvaully declare in all files.. PLz Gide a One step more.. Thanks for reply.. ---Sumit Kapoor--- sumit_kapoor1980@hotmail.com

          R 1 Reply Last reply
          0
          • S Sumit Kapoor

            Ya..Dear, Ya Dear your are right, I'm facing problem in release version,not in Debug. But can you give more clear idea..how to do? // Take these as Global CCriticalSection _bStopRightDownThread; bool bStopRightDownThread; ..... ..... ..... // In particular function use as follow _bStopRightDownThread.Lock(); bStopRightDownThread = true; _bStopRightDownThread.Unlock(); Do Your means this? If yes..then I'm using this in many files, DO u means I've to share Critical section vaiable to all using: extern CCriticalSection _bStopRightDownThread; or induvaully declare in all files.. PLz Gide a One step more.. Thanks for reply.. ---Sumit Kapoor--- sumit_kapoor1980@hotmail.com

            R Offline
            R Offline
            Ryan Binns
            wrote on last edited by
            #5

            If you are sharing a global variable, then you must share the critical section as well. Each function must lock the same critical section, otherwise they don't do anything. So you'll have to define it as extern if it's used in multiple files.

            Ryan

            "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups