Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

C++ class problem

  • 15-06-2006 01:41PM
    #1
    Registered Users, Registered Users 2 Posts: 250 ✭✭


    Hi

    I am experiencing a problem in a class that I've written and I am not sure why I am getting it. The class is supposed to load the library when it's instantiated and not again until the next reload of the class.

    I wrote a simple class to load an API library which is as follow:

    Class
    class MyClass
    {
    public:
      void LoadLibrary();
    private:
      BOOL bLibLoaded;
    }
    

    Constructor:
    MyClass::MyClass() { bLibLoaded = FALSE; }
    

    Function:
    void MyClass::LoadLibrary()
    {
      HMODULE hModule;
    
       if (bLibLoaded == FALSE)
      {
        hModule = LoadLibrary("mymodule.dll");
    
        bLibLoaded = TRUE;
      }
    }
    

    The problem is that I get an unhandled exception on the IF when it tests whether bLibLoaded is TRUE. I don't understand it. Initially I moved the hModule var declaration into the public/private sections of the class definition but it still doesn't want to work. If I remove the IF the code runs ok.

    This is the error message:
    First-chance exception in testclient.exe (BOCL.DLL): 0xC0000005: Access Violation.
    

    Naturally there is other code embedded aswell but I cannot see that that would effect this specific function.

    Any ideas?


Comments

  • Registered Users, Registered Users 2, Paid Member Posts: 2,427 ✭✭✭ressem


    Any more information in the stack trace?

    If you drop in more statements before 'bLibLoaded = TRUE;' does the error still occur.

    E.g. if you add in a typical bit of code, following the LoadLibrary

    if (null== hModule){
    //Log Error
    // return/exit/whatever
    }
    else {
    bLibLoaded = TRUE;
    }

    does the exception occur elsewhere, or the same line.


Advertisement