[Trilinos-Users] Teuchos::RCP behavior

Holger Brandsmeier holger.brandsmeier at sam.math.ethz.ch
Fri May 10 07:24:54 MDT 2013


Andrea,

indeed you are running in serious trouble here ...

> void assign(Teuchos::RCP<UInt> & P)
> {
>   UInt i = 4;
>   P = Teuchos::rcpFromRef(i);
>   cout << *P << endl;
> }
So you generate a class i of type "UInt" and P is a pointer on that
piece of memory. Once `assign` exists `i` is deleted and `P` referes
to invalid memory. This is bad.

Why don't you do:
void assign(Teuchos::RCP<UInt> & P)
{
   UInt* i = new UInt();
   *i = 4;
   P = Teuchos::rcp(i);
   cout << *P << endl;
 }

Or at least tell us what you try to achieve.

-Holger


On Fri, May 10, 2013 at 3:15 PM, Villa Andrea (RSE)
<Andrea.Villa at rse-web.it> wrote:
>
> I've implemented a very simple test code:
>
> void assign(Teuchos::RCP<UInt> & P)
> {
>   UInt i = 4;
>   P = Teuchos::rcpFromRef(i);
>   cout << *P << endl;
> }
>
> int main(int argc, char *argv[])
> {
>   Teuchos::RCP<UInt> P;
>   assign(P);
>
>   cout << *P << endl;
>   cout << "count: " << P.total_count() << endl;
> }
>
> I think that this is a non-correct usage of the RCP class, since a temporary
> reference is passed to P. In fact I get the following output:
>
> 4
> 0
> count: 1
>
> However even I have built Trilinos using
> -D Teuchos_ENABLE_DEBUG:BOOL=ON \
> -D Teuchos_ENABLE_DEBUG_RCP_NODE_TRACING:BOOL=ON \
> no exception is thrown. Am I missing something?
>
> Thank you very much.
> Andrea Villa.
>
>
> RSE SpA ha adottato il Modello Organizzativo ai sensi del D.Lgs.231/2001, in
> forza del quale l'assunzione di obbligazioni da parte della Società avviene
> con firma di un procuratore, munito di idonei poteri. RSE adopts a
> Compliance Programme under the Italian Law (D.Lgs.231/2001). According to
> this RSE Compliance Programme, any commitment of RSE is taken by the
> signature of one Representative granted by a proper Power of Attorney.
>
> Le informazioni contenute in questo messaggio di posta elettronica sono
> riservate e confidenziali e ne e' vietata la diffusione in qualsiasi modo o
> forma. Qualora Lei non fosse la persona destinataria del presente messaggio,
> La invitiamo a non diffonderlo e ad eliminarlo, dandone gentilmente
> comunicazione al mittente. The information included in this e-mail and any
> attachments are confidential and may also be privileged. If you are not the
> correct recipient, you are kindly requested to notify the sender
> immediately, to cancel it and not to disclose the contents to any other
> person.
>
> _______________________________________________
> Trilinos-Users mailing list
> Trilinos-Users at software.sandia.gov
> http://software.sandia.gov/mailman/listinfo/trilinos-users
>



-- 
Holger Brandsmeier, SAM, ETH Zürich
http://www.sam.math.ethz.ch/people/bholger



More information about the Trilinos-Users mailing list