-2

I have this one class: say "InterClass". This class is declared in say, MainClass as:

in MainClass.h :

public:
    InterClass *m_pInterClass; 

and in Constructor of MainClass:

m_pInterClass = new InterClass(this);

I want to use some functions of MainClass in InterClass, so passing this in constructor. I assign this to a global pointer and use it everywhere in class.

In main() everything works fine until it reaches to end.

Last lines of my code are:

delete MainClass;
OutputDebugString("Exiting Application");
return 0;

My Application crashes at "return 0" I put OutputDebugString() everywhere in my code. What I narrowed down is: On DebugView- I can see Destructor of "InterClass" getting executred then "Exiting Application" and then again Destructor of "InterClass" executes. I am confused why destructor of InterClass gets executed twice? that to at return 0;

I can't put breakpoint in this application due to nature of this application.

P.S. I am an Embedded C programmer and completely new to C++ (who is forced to work on PC application :( )

sbi
  • 219,715
  • 46
  • 258
  • 445
Swanand
  • 4,027
  • 10
  • 41
  • 69
  • 2
    Can we see a small, complete sample of relevant parts? – chris Jun 21 '12 at 14:05
  • Do you have a static instance of `InterClass` somewhere? – J-16 SDiZ Jun 21 '12 at 14:07
  • @J-16SDiZ No...No Static instance! – Swanand Jun 21 '12 at 14:09
  • 5
    You don't need to post sensitive code; start from scratch or pare down and create a [SSCCE](http://sscce.org). The process will likely answer your question without our help… – Potatoswatter Jun 21 '12 at 14:09
  • @SwanandPurankar, Is it possible to just try it with small, fake classes that have the same effect? That's what we'd want to see. – chris Jun 21 '12 at 14:09
  • @Potatoswatter: Sorry... Updated the post! – Swanand Jun 21 '12 at 14:11
  • 5
    Have you obeyed the [Rule Of Three](http://stackoverflow.com/questions/4172722/what-is-the-rule-of-three)? – sbi Jun 21 '12 at 14:11
  • 1
    As it stands, the question isn't really bad (anymore), so `+1` from me. If you indeed forgot to define a suitable copy constructor/assignment operator (see Rule of Three), you should _accept_ Als' answer. What book are you learning C++ for? (You cannot pick up C++ "on the go", you need to study thoroughly, preferably from a book.) Your terminology seems confused. (For example, there's no destructor called for a class, only for objects, which are instances of the class.) There's lots of _really_ bad books out there. You will need [a good C++ book](http://stackoverflow.com/q/388242/140719). – sbi Jun 21 '12 at 18:48

1 Answers1

4

Clearly, this smells of not obeying the Rule of Three.
Your class Copy constructor and Copy assignment operator should do a deep copy of the dynamically allocated member pointer. If not then you are bound to see the behavior you get.

Either way you should avoid using a raw pointer member to being with.You are much better off wrapping it up in a Smart pointer.

Community
  • 1
  • 1
Alok Save
  • 202,538
  • 53
  • 430
  • 533