[Insight-users] array of smart pointers
Xiaofeng Z
xf10036 at hotmail.com
Fri Oct 16 08:52:46 EDT 2009
The statement:
recv_images[ split ] = FloatImageType::New()
involves two objects: the smart pointer object and the FloatImageType object. It says to let the smart pointer object to manage the newly created FloatImageType object. In doing so, the smarter pointer will have to release the object that is previously under its supervision. If recv_images[ split ] contains some junk, it (the smart pointer object resides in recv_images[ split ]) will mistakenly think it is managing another object and will attempt to dealloc/dereference that (non-existing) object, hence causing segment fault.
----------------------------------------
> Date: Fri, 16 Oct 2009 05:22:29 -0400
> From: hobbsk at ohiou.edu
> To: xf10036 at hotmail.com
> CC: insight-users at itk.org
> Subject: Re: [Insight-users] array of smart pointers
>
> On 10/15/2009 11:41 PM, Xiaofeng Z wrote:
>> Did you initialized the allocated memory?
>
> I thought FloatImageType::New(); was supposed to do that.
>
>> Besides, unless you know the inner details of the smarter pointer,
>> you don't really know how to initialize the memory.
>>
>
> Clearly I do not know how to initialize the memory.
>
> Clearly FloatImageType::New(); does not completely set everything in a
> block of memory the size of a FloatImageType::Pointer.
>
> I guess this fits with this newfangled C++ idea of a constructor,
> declaring a FloatImageType::Pointer will lead to an object that is NOT
> filled with junk, but filled with some defined values that say "I'm an
> empty image smart pointer"
>
> Anyway just replacing my malloc lines with std::vector<
> FloatImageType::Pointer> recv_images( mpi_size ); worked just fine.
>
>> --------------------------------------------------
>> From: "Kevin H. Hobbs"
>> Sent: Thursday, October 15, 2009 1:54 PM
>> To: "Insight Users List"
>> Subject: [Insight-users] array of smart pointers
>>
>>> My (unfinished and attached) program is segfaulting at line 192 where I
>>> create a new image smart pointer and assign it to an element of an array
>>> of smart pointers.
>>>
>>> recv_images[ split ] = FloatImageType::New();
>>>
>>> I got the array of smart pointers from malloc with :
>>>
>>> FloatImageType::Pointer * recv_images =
>>> (FloatImageType::Pointer *) malloc(
>>> mpi_size * sizeof( FloatImageType::Pointer ));
>>>
>>> I know this isn't very C++ish but shouldn't it work?
>>>
>>
>>
>>
>>> _____________________________________
>>> Powered by www.kitware.com
>>>
>>> Visit other Kitware open-source projects at
>>> http://www.kitware.com/opensource/opensource.html
>>>
>>> Please keep messages on-topic and check the ITK FAQ at:
>>> http://www.itk.org/Wiki/ITK_FAQ
>>>
>>> Follow this link to subscribe/unsubscribe:
>>> http://www.itk.org/mailman/listinfo/insight-users
>>>
>
>
_________________________________________________________________
Hotmail: Trusted email with Microsoft’s powerful SPAM protection.
http://clk.atdmt.com/GBL/go/177141664/direct/01/
More information about the Insight-users
mailing list