re-allocating arrays in /clr

  • Thread starter Thread starter Peter Oliphant
  • Start date Start date
P

Peter Oliphant

I'm using VS C++.NET 2005 Express in /clr mode. Say I have a PointF array:

array<PointF>^ point_array = gcnew array<PointF>(100) ;

Now I want to re-allocate point_array, and in doing so also free up the
previous array's points. Is this sufficient:

point_array = gcnew array<PointF>(999) ; // new array

or do I need to tell the system I'm doing this, ala something like:

delete point_array ;
point_array = nullptr ;

I'm guessing just re-allocating (or pointing) to something new will
automatically free up the elements in the array since the elements
themselves are instances of a ref class. But I want to be sure.

I'll point out that both of the following lines compile, but I don't believe
they both would work when executed (i.e., one or both would cause a program
crash):

delete point_array ;
delete [] point_array ;

and I'm not sure, but probable just doing:

point_array = nullptr ;

or

point_array = some_other_point_array ;

would also free up the points that were in the original array, and in the
case of setting it to nullptr would make the variable 'point_array'
available for re-allocation. Note, I'm not concerned whether garbage
collection decides to immediately free up the resource memory, just that
it's legal for me to re-assign or re-allocate the pointer to the array
without causing problems.

Thanks in advance for any responses!

[==P==]
 
You can do a delete before doing the gcnew - but since the array does not
have a destructor, it won't really serve any purpose. Leave it to the GC to
collect the orphaned array as and when it pleases to do so :-)
 
Back
Top