| CodeGuru Home | VC++ / MFC / C++ | .NET / C# | Visual Basic | Newsletters | VB Forums | Developer.com |
|
|||||||
| Managed C++ and C++/CLI Discuss Managed C++ and .NET-specific questions related to C++. |
![]() |
|
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
System.NullReferenceException error in VC++.NET
Hi,
I have a problem which I dont know whether its to do with proper construction of an object or with releasing memory in a destructor. What I want to be able to do is have for example an array of strings so I can do the following pszChannel[0] = "Hello"; pszChannel[1] = "World"; pszChannel[2] = "!!!!!"; so I can do for example strcpy(m_pObject->pszChannel[0], "HELLO"); in another class. I have created an object which has the following properties #define MAX_NUM_CHANS = 3 #define MAX_STRING_LEN = 32 char* pszChannel[MAX_NUM_CHANS+1]; // IS IT OK char* pszPart[MAX_NUM_CHANS+1]; // TO DO THIS char* pszRange[MAX_NUM_CHANS+1]; // CONSTRUCTION char szName[MAX_STRING_LEN+1]; // ??????? SAFEARRAY *psaDataArray; What I want is to have the ability for the object to contain the following data for example in the default contructor CTH::CTH() { for (int i=0; ipszChannel[i] = new char[96]; strcpy(this->pszChannel[i], ""); this->pszPart[i] = new char[96]; strcpy(this-> pszPart [i], ""); this->pszRange[i] = new char[96]; strcpy(this-> pszRange [i], ""); } this->szName = “Rotate”; } I can create the object fine and populate the data properties OK but when I want to delete the object how do I delete the properties in the destructor ? What I ultimately require is to delete the object and free up all memory associated with this object. If I try the following in the destructor I get an error CTH::~CTH() { for (int i=0; ipszChannel[i]; delete [] this->pszPart[i]; delete [] this->pszRange[i]; } this->szName = “”; ::SafeArrayDestroy(psaDataArray); } ========== Exception Text ========== System.NullReferenceException: Value null was found where an instance of an object was required. at THLib.THIT.FileClose() at THTest.Form1.Button1_Click(Object sender, EventArgs e) in C:\1\source\THTest\Form1.vb:line 92 at System.Windows.Forms.Control.OnClick(EventArgs e) at System.Windows.Forms.Button.OnClick(EventArgs e) at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ButtonBase.WndProc(Message& m) at System.Windows.Forms.Button.WndProc(Message& m) at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) |
![]() |
| Bookmarks |
|
||||||
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|