Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4267826
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T06:57:08+00:00 2026-05-21T06:57:08+00:00

I have a global variable which is an instance of a class. This class

  • 0

I have a global variable which is an instance of a class. This class created an image in its constructor (directX).

The problem is that I am getting an access violation at runtime but the code compiles. I think the problem is that the class constructor is being called before the initialisation done in the winmain function.

so what I want to know is

  1. has anyone encountered this problem and knows of a solution.

  2. What is the lifespan of a global, i know variables declared in a function are lost after it returns and that the compiler looks through the code the see if everything matches which is why we have to prototype functions but where do global’s come into the equation.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T06:57:09+00:00Added an answer on May 21, 2026 at 6:57 am

    You probably want to look at something like the singleton pattern if you really want to have one instance of a global, that can be initialized after the initialisation is done (essentially, the image would be constructed the first time you referenced it after which you’d use the pre-constructed version).

    Globals are constructed (in undefined order), before your winmain is called. They stay there until your program exits (at which point I believe the destructors are called in an undefined order)..

    Another (possibly simpler) alternative you could use would be to change your global from an instance of the class to a pointer to it… then you’d have something like:

    // global...
    MyGlobalClass *bigGlobalImageHolder;
    
    // Winmain
    // Perform directX setup (don't know what that is)
    // Create the image class
    bigGlobalImageHolder = new MyGlobalClass();
    
    
    // do the rest of your stuff...  I'm guessing enter some kind of event loop
    
    // clean up your global
    delete bigGlobalImageHolder;
    // exit your winmain (and application)
    

    Then everywhere you’re currently referencing your global, you could reference it via a pointer instead..

     // so
     bigGlobalImageHolder.GetImage();
     // becomes
     bigGlobalImageHolder->GetImage();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class Page that creates an instance of DB , which is
I have a python module that makes use of a huge dictionary global variable,
I have a module named module.py, which checks a global variable in context. module.py:
so I have this function which gets called multiple times during my program. //global
I have created a C++-wrapper (a class) for a larger C codebase which was
I have created my own simple logger class which uses COM interop to redirect
I have a class that subclasses UIView called DrawView. This class contains custom drawing
I have used a static global variable and a static volatile variable in file
I have some global variables in a Python script. Some functions in that script
I have a big list of global variables that each have their own setup

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.