CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Aug 2001
    Posts
    42

    memory access error

    I have two static global instances (say gO1 and gO2)of two different classes, gO1 is in the Library and gO2 is in the main application. What happens is, after the execution of main(), the destructor for the gO1 is called first and it cleans up the allocated memory. Then destructor for the gO2 is called. The destructor of gO2 is making a call to the library which in turn tries to access the gO1. Since it was freed already it's raising an application error.
    Please suggest how better to resolve this issue.

    Thanks in advance

  2. #2
    Join Date
    May 2005
    Posts
    99

    Re: memory access error

    If your gO2 is dependant on gO1 and its not vice versa then why not delete gO2 first and then gO1. I know that you might have tried this but if this is not the solution then could you tell us more about this dependency of gO1 and gO2.

  3. #3
    Join Date
    Aug 2001
    Posts
    42

    Re: memory access error

    I'm not controlling the deletion sequence. Since the instances are in the global space, the destructors are called automatically after the execution of main.

  4. #4
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Thumbs up Re: memory access error

    Aaahh..have you read about the Static Initialization order problem? Do read up on that - there's a book C++ FAQs ..its explains the scenario pretty well and helps in carving the solutions out of it.

    You could also find this discussed in the online FAQ Lite - Marshall Cline. Here is the link to the FAQ 10.12 to 10.16. It will help you understand whats happening better and then you would be able to sort it out urself. The book deals with it in a better way...I am not sure about the online version. But, for now, go ahead.

  5. #5
    Join Date
    Jun 2003
    Location
    India
    Posts
    118

    Re: memory access error

    Does gO1 and gO2 linked ??

    Did gO2 is inherited from gO1 or something like this?

    -PiyuNewe

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured