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
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Initialize variable in static library [modified]

Initialize variable in static library [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
c++help
11 Posts 6 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.
  • S Offline
    S Offline
    samira forooghi
    wrote on last edited by
    #1

    hi every one I create a static library with visual c++ 6.0 App wizard. I create a new generic class and declared my variables in its header file. Then I initialized my variables in class constructor. I want to use it in a new dialog based project, so I add related header file and write my library name in linker tab at project setting. I create one object of this class in my function, But at the end of function when application exits from function occurs an exception. I can't handle this exception. Note : when I comment initialized this exception don’t happen. Please help me //////////////////////////////////////////////////////////////// // static library class (Test.h) ///////////////////////////////////////////////////////////////

    class CTest
    {
    public:

    CTest();
    virtual ~CTest();
    
    int		 m\_i1;//or INT   
    int		 m\_i2;   
    
        bool		 m\_b1;//or BOOL
    bool		 m\_b2;
    
    unsigned int	 m\_ui1;//or DWORD32
    unsigned int	 m\_ui2;
    
    unsigned short   m\_us1;//or USHORT
    unsigned short   m\_us2;
    
    BYTE		 m\_bte1;//#include  in stdafx.h for BYTE data type
    BYTE		 m\_bte2;
    
    CString	 m\_str1;//#include  in stdafx.h for CString data type
    CString	 m\_str2;	
    

    };

    // (Test.cpp)
    ///////////////////////////////////////////////////////////////
    #include "stdafx.h"
    #include "Test.h"

    //////////////////////////////////////////////////////////////////////
    // Construction/Destruction
    //////////////////////////////////////////////////////////////////////

    CTest::CTest()
    {

    m\_i1=0;
    m\_i2=0;  
    	
    m\_b1=true;
    m\_b2=true;
    	
    m\_ui1=0;
    m\_ui2=0;
    	
    m\_us1=1;
    m\_us2=1;
    	
    m\_bte1=0;
    m\_bte2=0;
    	
    m\_str1="";
    m\_str2="";
    

    }

    CTest::~CTest()
    {

    }

    //////////////////////////////////////////////////////////////// // My dialog base application ///////////////////////////////////////////////////////////////

    void CUseTestLibDlg::OnBinitlib()
    {
    CTest *pcTest=new CTest();

        delete pcTest;
    pcTest=NULL;
    

    }// Unhandled exception in UseTestLib.exe: 0xC0000005: Access Violation.

    void CUseTestLibDlg::OnBinitlib2()
    {
    CTest pcTest;

    }// Unhandled exception in UseTestLib.exe: 0xC0000005: Access Violation.

    modified on Wednesday, April 29, 2009 6:26 AM

    CPalliniC S 2 Replies Last reply
    0
    • S samira forooghi

      hi every one I create a static library with visual c++ 6.0 App wizard. I create a new generic class and declared my variables in its header file. Then I initialized my variables in class constructor. I want to use it in a new dialog based project, so I add related header file and write my library name in linker tab at project setting. I create one object of this class in my function, But at the end of function when application exits from function occurs an exception. I can't handle this exception. Note : when I comment initialized this exception don’t happen. Please help me //////////////////////////////////////////////////////////////// // static library class (Test.h) ///////////////////////////////////////////////////////////////

      class CTest
      {
      public:

      CTest();
      virtual ~CTest();
      
      int		 m\_i1;//or INT   
      int		 m\_i2;   
      
          bool		 m\_b1;//or BOOL
      bool		 m\_b2;
      
      unsigned int	 m\_ui1;//or DWORD32
      unsigned int	 m\_ui2;
      
      unsigned short   m\_us1;//or USHORT
      unsigned short   m\_us2;
      
      BYTE		 m\_bte1;//#include  in stdafx.h for BYTE data type
      BYTE		 m\_bte2;
      
      CString	 m\_str1;//#include  in stdafx.h for CString data type
      CString	 m\_str2;	
      

      };

      // (Test.cpp)
      ///////////////////////////////////////////////////////////////
      #include "stdafx.h"
      #include "Test.h"

      //////////////////////////////////////////////////////////////////////
      // Construction/Destruction
      //////////////////////////////////////////////////////////////////////

      CTest::CTest()
      {

      m\_i1=0;
      m\_i2=0;  
      	
      m\_b1=true;
      m\_b2=true;
      	
      m\_ui1=0;
      m\_ui2=0;
      	
      m\_us1=1;
      m\_us2=1;
      	
      m\_bte1=0;
      m\_bte2=0;
      	
      m\_str1="";
      m\_str2="";
      

      }

      CTest::~CTest()
      {

      }

      //////////////////////////////////////////////////////////////// // My dialog base application ///////////////////////////////////////////////////////////////

      void CUseTestLibDlg::OnBinitlib()
      {
      CTest *pcTest=new CTest();

          delete pcTest;
      pcTest=NULL;
      

      }// Unhandled exception in UseTestLib.exe: 0xC0000005: Access Violation.

      void CUseTestLibDlg::OnBinitlib2()
      {
      CTest pcTest;

      }// Unhandled exception in UseTestLib.exe: 0xC0000005: Access Violation.

      modified on Wednesday, April 29, 2009 6:26 AM

      CPalliniC Online
      CPalliniC Online
      CPallini
      wrote on last edited by
      #2

      samira forooghi wrote:

      pcTest=NULL; delete pcTest;

      I would invert the order of the statements... :rolleyes:

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
      [My articles]

      In testa che avete, signor di Ceprano?

      S S S 3 Replies Last reply
      0
      • CPalliniC CPallini

        samira forooghi wrote:

        pcTest=NULL; delete pcTest;

        I would invert the order of the statements... :rolleyes:

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
        [My articles]

        S Offline
        S Offline
        Stephen Hewitt
        wrote on last edited by
        #3

        Looks like good advice...

        Steve

        1 Reply Last reply
        0
        • CPalliniC CPallini

          samira forooghi wrote:

          pcTest=NULL; delete pcTest;

          I would invert the order of the statements... :rolleyes:

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
          [My articles]

          S Offline
          S Offline
          samira forooghi
          wrote on last edited by
          #4

          thanks for u i correct it ,but its not my problem.

          1 Reply Last reply
          0
          • CPalliniC CPallini

            samira forooghi wrote:

            pcTest=NULL; delete pcTest;

            I would invert the order of the statements... :rolleyes:

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
            [My articles]

            S Offline
            S Offline
            Stuart Dootson
            wrote on last edited by
            #5

            Shouldn't cause an accvio, though - you can delete NULL in C++.

            M S 2 Replies Last reply
            0
            • S Stuart Dootson

              Shouldn't cause an accvio, though - you can delete NULL in C++.

              M Offline
              M Offline
              Maximilien
              wrote on last edited by
              #6

              but will result in a memory leak.

              This signature was proudly tested on animals.

              S 1 Reply Last reply
              0
              • S samira forooghi

                hi every one I create a static library with visual c++ 6.0 App wizard. I create a new generic class and declared my variables in its header file. Then I initialized my variables in class constructor. I want to use it in a new dialog based project, so I add related header file and write my library name in linker tab at project setting. I create one object of this class in my function, But at the end of function when application exits from function occurs an exception. I can't handle this exception. Note : when I comment initialized this exception don’t happen. Please help me //////////////////////////////////////////////////////////////// // static library class (Test.h) ///////////////////////////////////////////////////////////////

                class CTest
                {
                public:

                CTest();
                virtual ~CTest();
                
                int		 m\_i1;//or INT   
                int		 m\_i2;   
                
                    bool		 m\_b1;//or BOOL
                bool		 m\_b2;
                
                unsigned int	 m\_ui1;//or DWORD32
                unsigned int	 m\_ui2;
                
                unsigned short   m\_us1;//or USHORT
                unsigned short   m\_us2;
                
                BYTE		 m\_bte1;//#include  in stdafx.h for BYTE data type
                BYTE		 m\_bte2;
                
                CString	 m\_str1;//#include  in stdafx.h for CString data type
                CString	 m\_str2;	
                

                };

                // (Test.cpp)
                ///////////////////////////////////////////////////////////////
                #include "stdafx.h"
                #include "Test.h"

                //////////////////////////////////////////////////////////////////////
                // Construction/Destruction
                //////////////////////////////////////////////////////////////////////

                CTest::CTest()
                {

                m\_i1=0;
                m\_i2=0;  
                	
                m\_b1=true;
                m\_b2=true;
                	
                m\_ui1=0;
                m\_ui2=0;
                	
                m\_us1=1;
                m\_us2=1;
                	
                m\_bte1=0;
                m\_bte2=0;
                	
                m\_str1="";
                m\_str2="";
                

                }

                CTest::~CTest()
                {

                }

                //////////////////////////////////////////////////////////////// // My dialog base application ///////////////////////////////////////////////////////////////

                void CUseTestLibDlg::OnBinitlib()
                {
                CTest *pcTest=new CTest();

                    delete pcTest;
                pcTest=NULL;
                

                }// Unhandled exception in UseTestLib.exe: 0xC0000005: Access Violation.

                void CUseTestLibDlg::OnBinitlib2()
                {
                CTest pcTest;

                }// Unhandled exception in UseTestLib.exe: 0xC0000005: Access Violation.

                modified on Wednesday, April 29, 2009 6:26 AM

                S Offline
                S Offline
                Stuart Dootson
                wrote on last edited by
                #7

                The problem's obviously in CTest's destructor. The only significant activity in CTest's destructor will be calls to m_str1's and m_str2's destructors. Therefore, the access violation is happening somewhere in there. I suspect there's something not quite right with your project settings - check that the library and main app are using hte same MFC and C run-time library settings.

                1 Reply Last reply
                0
                • S Stuart Dootson

                  Shouldn't cause an accvio, though - you can delete NULL in C++.

                  S Offline
                  S Offline
                  samira forooghi
                  wrote on last edited by
                  #8

                  i havent any problem with new delete , occur exception even this case

                  void CUseTestLibDlg::OnBinitlib2()
                  {
                  CTest pcTest;
                  }// Unhandled exception in UseTestLib.exe: 0xC0000005: Access Violation.

                  1 Reply Last reply
                  0
                  • M Maximilien

                    but will result in a memory leak.

                    This signature was proudly tested on animals.

                    S Offline
                    S Offline
                    Stuart Dootson
                    wrote on last edited by
                    #9

                    I didn't say it wouldn't - I was just saying that fixing it wouldn't solve the OP's problem.

                    S R 2 Replies Last reply
                    0
                    • S Stuart Dootson

                      I didn't say it wouldn't - I was just saying that fixing it wouldn't solve the OP's problem.

                      S Offline
                      S Offline
                      samira forooghi
                      wrote on last edited by
                      #10

                      Thanks a lot

                      1 Reply Last reply
                      0
                      • S Stuart Dootson

                        I didn't say it wouldn't - I was just saying that fixing it wouldn't solve the OP's problem.

                        R Offline
                        R Offline
                        Rajesh R Subramanian
                        wrote on last edited by
                        #11

                        Yes, but Carlo just indicated one of flaws in the OP's code. I agree that it wouldn't solve 'the problem' of the OP, but it might (hopefully) prevent the OP from shooting his own face sometime in the future. :)

                        It is a crappy thing, but it's life -^ Carlo Pallini

                        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