Well, for one thing, you can't do this:<br>+ newArray = static_cast<T*>(realloc(this->Array, sz*sizeof(T))); <br><br>if you eliminate all the use of malloc. realloc calls don't make any sense if you are using C++ new/delete.<br>
<br><br><br><div class="gmail_quote">2008/6/19 Burlen Loring <<a href="mailto:burlen.loring@kitware.com">burlen.loring@kitware.com</a>>:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Hi all,<br>
<br>
I would like to get some feed back on a change I would like to make to vtkDataArrayTemplate.txx.<br>
<br>
Here is the problem that prompted the changes:<br>
<br>
Currently during inserts, a data array resizes itself if need be. Unfortunately, in the case that it runs out of memory during the resize, it ignores the fact that it didn't get the needed space and writes into memory it doesn't own. For example take a look at how GetWritePointer is ignoring the return value from ResizeAndExtend which, will be 0 to indicate that malloc failed. And further how InsertNextTuple/InsertTuple has no way of knowing if the malloc failed. The insert then takes place into memory not owned by the data array.<br>
<br>
If you are unfortunate enough to use enough ram that malloc fails during a series of inserts, this can cause some very strange crashes!<br>
<br>
Attached is a patch I'd like to submit to cvs. What I have done is replace the malloc/free pairs with new/delete pairs. Failure of new is much easier to deal with (compared to malloc) because it throws a bad_alloc exception, which clearly indicates the nature of the problem, and the end user can decide how to handle that. Additionally the patch makes sure that if new returns 0 rather than throwing an exception(could happen on older compilers), a return value indicating an error occurred gets propagated back up to the caller immediately. For methods that return pointers the error value is 0 for methods that return vtkIdType the error value is -1. There are a few methods (eg SetTuple) that are typed as returning void, which simply return. In the case of new failures during an insert, this prevents the data array from writing into memory it doesn't own.<br>
<br>
Any objections/criticism/comments regarding this change?<br>
<br>
Thanks in advance<br>
Burlen<br><font color="#888888">
<br>
-- <br>
Burlen Loring<br>
Kitware, Inc.<br>
R&D Engineer<br>
28 Corporate Drive<br>
Clifton Park, NY 12065-8662<br>
Phone: 518-371-3971 x137<br>
<br>
</font><br>_______________________________________________<br>
vtk-developers mailing list<br>
<a href="mailto:vtk-developers@vtk.org">vtk-developers@vtk.org</a><br>
<a href="http://www.vtk.org/mailman/listinfo/vtk-developers" target="_blank">http://www.vtk.org/mailman/listinfo/vtk-developers</a><br>
<br></blockquote></div><br>