Click to See Complete Forum and Search --> : memory access error
r_kalidass
July 21st, 2005, 11:00 PM
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
manish_velankani
July 21st, 2005, 11:23 PM
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.
r_kalidass
July 21st, 2005, 11:40 PM
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.
exterminator
July 22nd, 2005, 12:14 AM
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 (http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.12). 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. :thumb:
PiyuNewe
July 22nd, 2005, 12:19 AM
Does gO1 and gO2 linked ??
Did gO2 is inherited from gO1 or something like this?
-PiyuNewe
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.