I have a program where one class has an object of another class as a member. There is an object of the first class as a global variable. When the program closes on the closure of the main window and the default destructors are called (on the uninitialised objects), I recieve an access violation. The problem can be solved by removing the object of the second class from the first class. I would prefer not to do this, or at least I would prefer to know why I have to do this!
The above scenario is not doomed to failure and there are specific features of my code that is causing the problem. If needed I can post the code, but am now off to bed. Any ideas?
(Searching has indicated that others have had similar problems, and one stand out is that a double delete is going on. If this is the case, what circumstances cause this since I cannot replicate the problem in simple codes!).
// First class, just keeps an int variable
class Class1
{
public:
Class1() {}
~Class1()
{
}
private:
int data;
};
// Second class. Keeps an instance of the first class.
class Class2
{
public:
Class2() {}
~Class2()
{
}
private:
Class1 class1Member;
};
// Global variable of Class2
Class2 globalC2;
int main()
{
// globalC2 = Class2(); // Uncomment if we want to initialise globalC2
// do something
return 0;
}
If that's the kind of thing you're talking about, I can't reproduce it. You said that it happens when the global variable isn't even initialised? Why would the destructor then be called when the program terminates? I set breakpoints at both destructors in the code above, and they didn't get hit. If one initialises the global variable in main, then the destructors get hit. Perhaps others know more about this case than me, but I'd need to see your code...
Wow, amit0991, how could you solve a problem that you don't have enough information to even start
thinking about? I, like sammy34, need more information before being able to venture a guess.
Thanks sammy34 - what you posted has caused me to solve the problem, though I still am not certain what the problem was! Sadly I still cannot reproduce the problem in simple code, but an overview of the complete code is included below if it is of interest to others.
The situation was almost as written, except that I did not explicitly include the destructors (I just let the automatic destructors do their work), there was a single initialised (by which I mean it had an assigned value) boolean variable in the constructor of the second class, and I was making a windows app and the second class includes, among other things, a third user defined class as shown in the code below. When I do explicitly include the destructors in the class the problem disappears (thanks again!). Whenever I tried to reproduce the problem in simpler situations, the failure to explicitly include destructors doesn't matter.
I guess alls well that ends well. But if anyone can verify the lack of explicit destructors can cause access violations or an explanation as to why this is, I would be happy to learn!
bool constV;
set<int> nodes;
map<int,set<int>> neigh; // nodes connected by undirected edge
map<int,set<int>> adj; // nodes connected by any edge
map<int,set<int>> par; // parents
map<int,set<int>> chi; // children
map<int,vector<int>> vals; // possible values for each variable
};
class LS
{
public:
LS();
bool check();
void loadData(tstring filename);
multiset<map<int,int>>& getData(); //note return by reference
DAG learn(int thread);
PDAG pdag;
private:
int ess;
bool firstlearn;
map<int,map<int,map<int,double>>> dirLib; //node,parent combo,value,dir int
multiset<map<int,int>> data; // map rather than vector permits any naming for nodes