[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 292
  • Last Modified:

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

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;
};
0
fenriss_wolff
Asked:
fenriss_wolff
  • 5
  • 4
1 Solution
 
fenriss_wolffAuthor 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.
0
 
AlexFMCommented:
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.
0
 
fenriss_wolffAuthor 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!
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
AlexFMCommented:
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.
0
 
fenriss_wolffAuthor 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. :)

0
 
AlexFMCommented:
Pointers allocated with new are not subject of garbage collection. You must release them, otherwise you create memory leaks.
0
 
fenriss_wolffAuthor 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 ';'
0
 
AlexFMCommented:
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 :)
0
 
fenriss_wolffAuthor 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!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now