|
-
July 21st, 2005, 11:00 PM
#1
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
-
July 21st, 2005, 11:23 PM
#2
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.
-
July 21st, 2005, 11:40 PM
#3
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.
-
July 22nd, 2005, 12:14 AM
#4
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.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
July 22nd, 2005, 12:19 AM
#5
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|