We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Help with System.NullReferenceException in simple event method, visual c++ .net

fenriss_wolff
on
Medium Priority
348 Views
Last Modified: 2010-04-24
I get this error:
An unhandled exception of type 'System.NullReferenceException' occurred in Information Passing2.exe

Additional information: Object reference not set to an instance of an object.


with the following section of pertinent code:
      private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
            //Pnt* pt = new Pnt[1000000];
                  array<String^>^ str1 = gcnew array<String^>(1000000);
                  array<Pnt^>^ pt = gcnew array<Pnt^>(1000000);
                  for (int i=0; i < 100; i++){
                        pt[i]->x = 0.0;
                        pt[i]->y = 0.0;
                        str1[i] = "test";
                        }
                  }

      };
}

Pnt is the following class:
#include "stdafx.h"
#pragma once
      using namespace System;
      using namespace System::ComponentModel;
      using namespace System::Collections;
      using namespace System::Windows::Forms;
      using namespace System::Data;
      using namespace System::Drawing;
ref class Pnt {
public:
      Double x;
      Double y;
};
Comment
Watch Question

Author

Commented:
Sorry, for some reason I didn't get all the cut and paste in there. This is visual c++ .net express 2005. I have tried it with plain old pointers and with the managed types that you see and still no luck. Can you point out what is wrong ? I don't understand what I am doing wrong in the syntax to get the error, it all compiles just fine.
Commented:
for (int i=0; i < 100; i++)
{
     pt[i] = gcnew Pnt();
     pt[i]->x = 0.0;
     pt[i]->y = 0.0;
     str[i] = gcnew string("test");
}

Allocating array is not enough, you need to allocate every array member. Initially array contans null references.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
Thanks Alex,

How do I "delete" the allocated pointers? I changed 100 to 1000000 and it looks like there is BIG jump in memory and it only seems to be GC'd when I minimize the window? Anyway, thanks and you get the point. Funny thing is I just found the same on an excellent article on

http://www.codeproject.com/managedcpp/cppcliarrays.asp

thanks!

Commented:
You don't need to delete them, this is function of Garbage Collector. It allows to application to consume maxumum memory before it starts collecting. However, if your program really needs memory for something else, GC makes collecting to free memory.
We only need to think about this if our objects use some unmanaged resources like database connections, file handles etc.

Author

Commented:
Hmmm I thank you for your responses. I noticed thought that the garbage collection for using managed pointers (^) allocated with gcnew is much more agressive than that of (*) allocate with new (and there is no delete of course, like you mentioned) . That's the part I don't understand. Why did minimizing have to be done? I saw my example gobble up 250 megs and not get GC'd while when I used gcnew and ^ it reached 50 megs at the most and after that was always collected faster than my clicking of the button. Just curious why new would be GC'd any less often than gcnew. Insights are always welcome. :)

Commented:
Pointers allocated with new are not subject of garbage collection. You must release them, otherwise you create memory leaks.

Author

Commented:
My point was that I could add something like this to a button event handler:

      private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
            //Pnt* pt = new Pnt[1000000];
                  array<String^>^ str1 = gcnew array<String^>(1000000);
                  array<Pnt^> ^pt = gcnew array<Pnt^>(1000000);
                  int *t= new int[10000000];
                  for (int i=0; i < 1000000; i++){
                        pt[i] = gcnew Pnt();
                        pt[i]->x = 0.0;
                        pt[i]->y = 0.0;
                        str1[i] = "test";
                        }
                  }
                  delete[] t;
      };

and if i also put in a delete of t the compiler will say, so my point is if you can new a pointer why can't you delete it? Is there some other keyword, or does that pointer live and leak memory until the program exits, because you can't use a delete, there has to be SOME way to delete it, whether by GC or keyword, of course this is all in managed mode:

c:\documents and settings\x\my documents\visual studio 2005\projects\information passing2\information passing2\Form1.h(108) : error C2059: syntax error : 'delete'
c:\documents and settings\x\my documents\visual studio 2005\projects\information passing2\information passing2\Form1.h(108) : error C2238: unexpected token(s) preceding ';'

Commented:
I tested this code, it is compiled:

    array<String^>^ str1 = gcnew array<String^>(1000000);
    int *t= new int[10000000];
    for (int i=0; i < 1000000; i++){
        str1[i] = gcnew String("test");
    }
    delete[] t;

Remove one } in the end of the loop - sometimes compilr messages are unpredicrable :)

Author

Commented:
hi. Yep that was it. Now I feel stupid :) . But at least I'm dedicated working on this on the weekend! I really have a lot of catching up to do. I programmed a while with c and embedded systems. Then c++ for about a year and now I am required to do some visual c++ .net. I'm glad I arrived when I did after some of the initial syntax was revised. Man that stuff is ugly :) . I suppose I was just too frustrated with the problem at hand I didn't look for stuff like missing braces. Thanks!
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.