Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Destructor Not Getting Called

Destructor Not Getting Called

Scheduled Pinned Locked Moved C / C++ / MFC
helpdata-structuresperformance
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    CoffeeAddict19
    wrote on last edited by
    #1

    I'm having a problem with a class I wrote- the destructor isn't being called for some reason and I'm getting a rather large memory leak as a result. I verified that the destructor was not being called through the use of breakpoints. Any help would be appreciated. Here is the class defenition:

    class CCharLinkedList
    {
    public:
    	CCharLinkedList();
    	~CCharLinkedList();
    	void AddNodeToHead(char DataToAdd[]);
    	void AddNodeToTail(char DataToAdd[]);
    	bool DeleteFromHead();
    	bool DeleteFromTail();
    	bool CopyFromHead(char DataToCopy[]);
    	bool CopyFromTail(char DataToCopy[]);
    	void PrintHeadToTail();
    	void PrintTailToHead();
    protected:
    private:
    	void DeleteList();
    	CharLinkedListNodeType* HeadPtr;
    	CharLinkedListNodeType* TailPtr;
    };
    

    The destructor implementation:

    CCharLinkedList::~CCharLinkedList()
    {
    
    	DeleteList();
    	HeadPtr = NULL;
    	TailPtr = NULL;
    }
    

    DeleteList():

    void CCharLinkedList::DeleteList()
    {
    	CharLinkedListNodeType* IterationPtr = HeadPtr;
    	CharLinkedListNodeType* DeletePtr = NULL;
    
    	while(IterationPtr != NULL)
    	{
    
    		DeletePtr = IterationPtr;
    		IterationPtr = IterationPtr->ForwardPtr;
    		delete DeletePtr;
    	}
    }
    

    The class is declared in the free store in another class header file. The other class has been working fine for a long time. So far I have been able to use the linked list to add nodes, but when it is time to call the destructor upon exiting and delete all the nodes, nothing happens.

    M J 2 Replies Last reply
    0
    • C CoffeeAddict19

      I'm having a problem with a class I wrote- the destructor isn't being called for some reason and I'm getting a rather large memory leak as a result. I verified that the destructor was not being called through the use of breakpoints. Any help would be appreciated. Here is the class defenition:

      class CCharLinkedList
      {
      public:
      	CCharLinkedList();
      	~CCharLinkedList();
      	void AddNodeToHead(char DataToAdd[]);
      	void AddNodeToTail(char DataToAdd[]);
      	bool DeleteFromHead();
      	bool DeleteFromTail();
      	bool CopyFromHead(char DataToCopy[]);
      	bool CopyFromTail(char DataToCopy[]);
      	void PrintHeadToTail();
      	void PrintTailToHead();
      protected:
      private:
      	void DeleteList();
      	CharLinkedListNodeType* HeadPtr;
      	CharLinkedListNodeType* TailPtr;
      };
      

      The destructor implementation:

      CCharLinkedList::~CCharLinkedList()
      {
      
      	DeleteList();
      	HeadPtr = NULL;
      	TailPtr = NULL;
      }
      

      DeleteList():

      void CCharLinkedList::DeleteList()
      {
      	CharLinkedListNodeType* IterationPtr = HeadPtr;
      	CharLinkedListNodeType* DeletePtr = NULL;
      
      	while(IterationPtr != NULL)
      	{
      
      		DeletePtr = IterationPtr;
      		IterationPtr = IterationPtr->ForwardPtr;
      		delete DeletePtr;
      	}
      }
      

      The class is declared in the free store in another class header file. The other class has been working fine for a long time. So far I have been able to use the linked list to add nodes, but when it is time to call the destructor upon exiting and delete all the nodes, nothing happens.

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      You've shown the code for the class but it's irrelevent to the problem. What's important is the creation and destruction of the CCharLinkedList object(s). Are they created/destroyed with new/delete? Are they member variables in another class? If so, when does the object of THAT class get deleted or go out of scope? etc... Mark

      "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

      C 1 Reply Last reply
      0
      • M Mark Salsbery

        You've shown the code for the class but it's irrelevent to the problem. What's important is the creation and destruction of the CCharLinkedList object(s). Are they created/destroyed with new/delete? Are they member variables in another class? If so, when does the object of THAT class get deleted or go out of scope? etc... Mark

        "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

        C Offline
        C Offline
        CoffeeAddict19
        wrote on last edited by
        #3

        I only have one CCharLinkedList object. It is allocated on the free store (not new/delete), here:

        class CRatiosWin : public CFrameWnd
        {
        public:
        	CRatiosWin(); //default constructor
        	~CRatiosWin(); //destructor
        	CRatiosLogic RatiosLogic;
        	bool GetNewFilePath(CString & Path);
        	void PrintResults(string results);
        	//accessor functions
        	bool ReturnValidFilePathSet();
        	//manipulator functions
        	void ResetSearchDialogFlag(); //set that the search dialog has not been opened
        	void SetDatabaseFilePathStatus(bool FilePathWasSet);
        	void SetTimeParameterStatus(bool NewTimeParametersWereSet);
        protected:
        private:
        	//classes
        	CRatiosNewSearchDialog NewSearchDialog;
        	CRatiosSearchTimeParameters TimeParametersDialog;
        	CCharLinkedList TestLinkedList; :(
        	//other private members
        	CEdit ResultsBox;
        	BOOL NewSearchDialogIsVisible; //if TRUE, the NewSearchDialog is active
        	bool ValidFilePathSet;
        	bool TimeParametersWereSet;
        	afx_msg void OnClose();
        	afx_msg void OnOpenFile();
        	afx_msg void OnNewSearch();
        	afx_msg void OnOpenSearch();
        	afx_msg void OnAbout();
        	afx_msg void OnTestList();
        DECLARE_MESSAGE_MAP()
        };
        

        I loaded some text into the list by calling this at runtime:

        afx_msg void CRatiosWin::OnTestList()
        {
        	fstream ListFileStream;
        	char chLine[MAXNUMCHARS_LINKLISTNODE];
        	string strLine = "";
        
        	ListFileStream.open("list_test.txt");
        	while(ListFileStream.eof() == false)
        	{
        		getline(ListFileStream, strLine);
        		ASSERT(strLine.length() < MAXNUMCHARS_LINKLISTNODE);
        		strcpy(chLine, strLine.c_str());
        		TestLinkedList.AddNodeToTail(chLine);
        	}
        	MessageBox("Sent to file.", "Boink", MB_OK);
        	ListFileStream.close();
        }
        

        All of the above worked fine. But I set breakpoints in the destructor in the previous post and none of them were reached, plus I have a big memory leak.

        M 1 Reply Last reply
        0
        • C CoffeeAddict19

          I only have one CCharLinkedList object. It is allocated on the free store (not new/delete), here:

          class CRatiosWin : public CFrameWnd
          {
          public:
          	CRatiosWin(); //default constructor
          	~CRatiosWin(); //destructor
          	CRatiosLogic RatiosLogic;
          	bool GetNewFilePath(CString & Path);
          	void PrintResults(string results);
          	//accessor functions
          	bool ReturnValidFilePathSet();
          	//manipulator functions
          	void ResetSearchDialogFlag(); //set that the search dialog has not been opened
          	void SetDatabaseFilePathStatus(bool FilePathWasSet);
          	void SetTimeParameterStatus(bool NewTimeParametersWereSet);
          protected:
          private:
          	//classes
          	CRatiosNewSearchDialog NewSearchDialog;
          	CRatiosSearchTimeParameters TimeParametersDialog;
          	CCharLinkedList TestLinkedList; :(
          	//other private members
          	CEdit ResultsBox;
          	BOOL NewSearchDialogIsVisible; //if TRUE, the NewSearchDialog is active
          	bool ValidFilePathSet;
          	bool TimeParametersWereSet;
          	afx_msg void OnClose();
          	afx_msg void OnOpenFile();
          	afx_msg void OnNewSearch();
          	afx_msg void OnOpenSearch();
          	afx_msg void OnAbout();
          	afx_msg void OnTestList();
          DECLARE_MESSAGE_MAP()
          };
          

          I loaded some text into the list by calling this at runtime:

          afx_msg void CRatiosWin::OnTestList()
          {
          	fstream ListFileStream;
          	char chLine[MAXNUMCHARS_LINKLISTNODE];
          	string strLine = "";
          
          	ListFileStream.open("list_test.txt");
          	while(ListFileStream.eof() == false)
          	{
          		getline(ListFileStream, strLine);
          		ASSERT(strLine.length() < MAXNUMCHARS_LINKLISTNODE);
          		strcpy(chLine, strLine.c_str());
          		TestLinkedList.AddNodeToTail(chLine);
          	}
          	MessageBox("Sent to file.", "Boink", MB_OK);
          	ListFileStream.close();
          }
          

          All of the above worked fine. But I set breakpoints in the destructor in the previous post and none of them were reached, plus I have a big memory leak.

          M Offline
          M Offline
          Mark Salsbery
          wrote on last edited by
          #4

          Cool thanks. In that case you need to look at when the CRatiosWin object is destroyed. It is then that the CCharLinkedList member object will be destroyed. Mark

          "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

          C 1 Reply Last reply
          0
          • M Mark Salsbery

            Cool thanks. In that case you need to look at when the CRatiosWin object is destroyed. It is then that the CCharLinkedList member object will be destroyed. Mark

            "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

            C Offline
            C Offline
            CoffeeAddict19
            wrote on last edited by
            #5

            Can I assume that CRatiosWin is not getting deleted in this segment of code?

            class CPRatiosApp : public CWinApp
            {
            	public:
            	BOOL InitInstance()
            	{
            		_CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
            		m_pMainWnd = new CRatiosWin;
            		m_pMainWnd->ShowWindow(m_nCmdShow);
            		m_pMainWnd->UpdateWindow();
            		m_pMainWnd->SetWindowText("Ratios v0.1b");
            		m_pMainWnd->MoveWindow(0,0,800,600);
            	return TRUE;
            	}
            } CRatiosApp;
            
            M 2 Replies Last reply
            0
            • C CoffeeAddict19

              Can I assume that CRatiosWin is not getting deleted in this segment of code?

              class CPRatiosApp : public CWinApp
              {
              	public:
              	BOOL InitInstance()
              	{
              		_CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
              		m_pMainWnd = new CRatiosWin;
              		m_pMainWnd->ShowWindow(m_nCmdShow);
              		m_pMainWnd->UpdateWindow();
              		m_pMainWnd->SetWindowText("Ratios v0.1b");
              		m_pMainWnd->MoveWindow(0,0,800,600);
              	return TRUE;
              	}
              } CRatiosApp;
              
              M Offline
              M Offline
              Mark Salsbery
              wrote on last edited by
              #6

              CoffeeAddict19 wrote:

              Can I assume that CRatiosWin is not getting deleted in this segment of code?

              Yes...correct. I can't find where m_pMainWnd is destroyed in the MFC source...When I find it I'll post. :rolleyes: Try adding this override to your CPRatiosApp class:

              virtual int ExitInstance()
              {
              if (m_pMainWnd)
              {
              m_pMainWnd->DestroyWindow();
              m_pMainWnd = 0;
              }

              return CWinApp::ExitInstance();
              }

              Your destructor should be called when the m_pMainWnd->DestroyWindow(); is executed. Mark

              "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

              1 Reply Last reply
              0
              • C CoffeeAddict19

                Can I assume that CRatiosWin is not getting deleted in this segment of code?

                class CPRatiosApp : public CWinApp
                {
                	public:
                	BOOL InitInstance()
                	{
                		_CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
                		m_pMainWnd = new CRatiosWin;
                		m_pMainWnd->ShowWindow(m_nCmdShow);
                		m_pMainWnd->UpdateWindow();
                		m_pMainWnd->SetWindowText("Ratios v0.1b");
                		m_pMainWnd->MoveWindow(0,0,800,600);
                	return TRUE;
                	}
                } CRatiosApp;
                
                M Offline
                M Offline
                Mark Salsbery
                wrote on last edited by
                #7

                Ok....after reviewing MFC source - Your destructors should get called when you close the main window (since frame windows are self- deleting). I found no place where the app classes destroy the main window explicitly. The ExitInstance() code I posted won't help. You'll need to close the main window before a breakpoint in your destuctor will be called :) Mark

                "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

                1 Reply Last reply
                0
                • C CoffeeAddict19

                  I'm having a problem with a class I wrote- the destructor isn't being called for some reason and I'm getting a rather large memory leak as a result. I verified that the destructor was not being called through the use of breakpoints. Any help would be appreciated. Here is the class defenition:

                  class CCharLinkedList
                  {
                  public:
                  	CCharLinkedList();
                  	~CCharLinkedList();
                  	void AddNodeToHead(char DataToAdd[]);
                  	void AddNodeToTail(char DataToAdd[]);
                  	bool DeleteFromHead();
                  	bool DeleteFromTail();
                  	bool CopyFromHead(char DataToCopy[]);
                  	bool CopyFromTail(char DataToCopy[]);
                  	void PrintHeadToTail();
                  	void PrintTailToHead();
                  protected:
                  private:
                  	void DeleteList();
                  	CharLinkedListNodeType* HeadPtr;
                  	CharLinkedListNodeType* TailPtr;
                  };
                  

                  The destructor implementation:

                  CCharLinkedList::~CCharLinkedList()
                  {
                  
                  	DeleteList();
                  	HeadPtr = NULL;
                  	TailPtr = NULL;
                  }
                  

                  DeleteList():

                  void CCharLinkedList::DeleteList()
                  {
                  	CharLinkedListNodeType* IterationPtr = HeadPtr;
                  	CharLinkedListNodeType* DeletePtr = NULL;
                  
                  	while(IterationPtr != NULL)
                  	{
                  
                  		DeletePtr = IterationPtr;
                  		IterationPtr = IterationPtr->ForwardPtr;
                  		delete DeletePtr;
                  	}
                  }
                  

                  The class is declared in the free store in another class header file. The other class has been working fine for a long time. So far I have been able to use the linked list to add nodes, but when it is time to call the destructor upon exiting and delete all the nodes, nothing happens.

                  J Offline
                  J Offline
                  John R Shaw
                  wrote on last edited by
                  #8

                  When I learned to program we had to write stuff like this – why are you? You should be using the ‘list’ template. Some of the modern MS languages allow you to ignore the fact that if you use ‘new’ you need to supply a ‘delete’, may be that is the problem. Any thing you allocate you need to deallocate – even in C++.

                  INTP "Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups