Initialize variable in static library [modified]
-
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
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] -
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]Looks like good advice...
Steve
-
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]thanks for u i correct it ,but its not my problem.
-
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]Shouldn't cause an accvio, though - you can delete NULL in C++.
-
Shouldn't cause an accvio, though - you can delete NULL in C++.
but will result in a memory leak.
This signature was proudly tested on animals.
-
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
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.
-
Shouldn't cause an accvio, though - you can delete NULL in C++.
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. -
but will result in a memory leak.
This signature was proudly tested on animals.
I didn't say it wouldn't - I was just saying that fixing it wouldn't solve the OP's problem.
-
I didn't say it wouldn't - I was just saying that fixing it wouldn't solve the OP's problem.
Thanks a lot
-
I didn't say it wouldn't - I was just saying that fixing it wouldn't solve the OP's problem.
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