[vtk-developers] Mac java not returning zero-initialized objects

Bill Lorensen bill.lorensen at gmail.com
Fri Feb 25 08:37:22 EST 2011


I just snooped around and it does seem that java always initializes
memory, even for large chunks of dynamically allocated memory.

Unfortunately, this is not a wrapping issue. It's a different
philosophy between C/C++ and Java.

On Fri, Feb 25, 2011 at 8:13 AM, Bill Lorensen <bill.lorensen at gmail.com> wrote:
> Are you positive that a large, dynamically allocated block of memory
> is always zeroed in Java?
>
> On Fri, Feb 25, 2011 at 1:11 AM, Mark Roden <mmroden at gmail.com> wrote:
>> Thanks to everyone on this.
>>
>> The solution by Dean fixed the issue.
>>
>> I do want to stress, though, that this is a big difference between
>> C/C++ and Java-- Java's guarantee of default initial values means that
>> many programmers rely on those defaults.  I think quite a few people
>> (myself included) will see stability problems because they will (and I
>> have) use that guarantee to assume that objects are initialized to
>> zeros, and therefore don't perform a secondary memory-wiping step once
>> an object has been allocated.  I don't pretend to know how hard it
>> would be to fix the wrapping, but it does mean that vtk's java
>> libraries do not conform to a basic portion of the java standard.
>>
>> Just raising the issue.
>>
>> Thanks for the help,
>> Mark
>>
>> On Thu, Feb 24, 2011 at 8:42 AM, David Gobbi <david.gobbi at gmail.com> wrote:
>>> Hi Mark,
>>>
>>> In VTK, when vtkDataArrays are allocated they are not initialized.
>>> Since VTK data array allocation occurs everywhere in VTK, often
>>> several layers deep, it would be impossible for the Java wrappers
>>> to know when a vtkDataArray is allocated.  Someone could add an
>>> exception specifically for vtkImageData::AllocateScalars(), but that
>>> would just be scratching an itch.
>>>
>>> You'll have to find some way to clear the scalars.  Try Dean's suggestion
>>> to see if it works for you.
>>>
>>>  - David
>>>
>>>
>>> On Thu, Feb 24, 2011 at 9:17 AM, Mark Roden <mmroden at gmail.com> wrote:
>>>> I've found what I think is an interesting bug in the Java wrapping.
>>>>
>>>> According to the spec
>>>> (http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.12.5),
>>>> values in java should be initialized to zero.  However, when I create
>>>> a new vtkImageData object, the contents are not zero.
>>>>
>>>> For instance, this method, used to make a mask on top of an existing
>>>> vtkImageData object:
>>>>
>>>> public vtkImageData getBlankBinaryData(vtkImageData inImage){
>>>>  vtkImageData blankImage = new vtkImageData();
>>>>  int [] extent = inImage.GetWholeExtent();
>>>>  double[] origin = inImage.GetOrigin();
>>>>  double[] spacing = inimage.GetSpacing();
>>>>  blankImage.SetExtent(extent);
>>>>  blankImage.SetOrigin(origin);
>>>>  blankImage.SetSpacing(spacing);
>>>>  blankImage.SetScalarTypeToUnsignedChar();
>>>>  blankImage.AllocateScalars();
>>>>  return blankImage;
>>>> }
>>>>
>>>> That above code returns an image that is set to zero on the PC, but
>>>> contains a wide variety of values on the mac.
>>>>
>>>> This seems like an oversight in the wrapper.  What should I do to
>>>> guarantee that a new vtkImageData contains only zeros?
>>>>
>>>> Thanks,
>>>> Mark
>>>
>> _______________________________________________
>> Powered by www.kitware.com
>>
>> Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html
>>
>> Follow this link to subscribe/unsubscribe:
>> http://www.vtk.org/mailman/listinfo/vtk-developers
>>
>>
>



More information about the vtk-developers mailing list