CFD Online Discussion Forums

CFD Online Discussion Forums (http://www.cfd-online.com/Forums/)
-   OpenFOAM Programming & Development (http://www.cfd-online.com/Forums/openfoam-programming-development/)
-   -   autoPtr variable becomes invalid (http://www.cfd-online.com/Forums/openfoam-programming-development/87904-autoptr-variable-becomes-invalid.html)

djpiro May 2, 2011 10:05

autoPtr variable becomes invalid
 
Hello,

I am having a problem with autoPtr variables that are no longer valid at a certain point in the code and I cannot figure out why. A description of the what happens is included below:

- I have a List<autoPtr<"Base Class"> > variable in an object
- In a method, I use the autoPtr.set(T*) method using the "new" operator of a class derived from "Base Class".
- I can check and all of the autoPtr's in the list are valid.
- The autoPtr's are dereferenced with no problem.
- A copy constructor is called on the object that contains the list, the list is copied, and the autoPtr's are checked and still valid.
- The autoPtr's are again dereferenced with no problem.
- A copy constructor is again called on the object that contains the list, the list is copied, and the autoPtr's are checked and still valid. (I do not have control over the number of times the object is copied - this is done above the level of the program I am modifying.)
- At this point the autoPtr's are no longer valid.

I cannot figure out where the pointers are becoming invalid, because no method is called on the object that contains the list of autoPtr's between the point where they are checked and are valid and checked again and invalid.

If anyone has any ideas on what could cause this, I would appreciate the help.

Thanks,
Dominic

olesen May 3, 2011 04:21

Quote:

Originally Posted by djpiro (Post 305944)
Hello,

I am having a problem with autoPtr variables that are no longer valid at a certain point in the code and I cannot figure out why. A description of the what happens is included below:

- I have a List<autoPtr<"Base Class"> > variable in an object

Why not use a PtrList<BaseClass> for storage instead? I think this might avoid many of your problems.

djpiro May 3, 2011 09:27

Mark,

Thank you for your reply. Unfortunately, using a PtrList, the pointers were no longer valid after the first copy constructor. This behavior is at least more predictable, but not desirable.

Dominic

rudolf.hellmuth January 14, 2013 08:35

The same has happened to me.

I am coding a chemistry model for liquid solutions. The user defines the number of solutes and reactions. Therefore it has

Code:

PtrList<Solute> solution::soluteList;
PtrList<Reactions> solution::chemistry::reactionList;

Solute class has a
Code:

autoPtr<volScalarField> C
to store the concentration field.

chemistry generates the solutes' source fields and reaction needs to know the solutes concentration to generate the reaction rate's fields. I wanted to have the reference to soluteList at chemistry and each reaction, but the concentration content is deleted as described by Dominic above.

djpiro January 14, 2013 09:33

Quote:

Originally Posted by rudolf.hellmuth (Post 401723)
The same has happened to me.

I am coding a chemistry model for liquid solutions. The user defines the number of solutes and reactions. Therefore it has

Code:

PtrList<Solute> solution::soluteList;
PtrList<Reactions> solution::chemistry::reactionList;

Solute class has a
Code:

autoPtr<volScalarField> C
to store the concentration field.

chemistry generates the solutes' source fields and reaction needs to know the solutes concentration to generate the reaction rate's fields. I wanted to have the reference to soluteList at chemistry and each reaction, but the concentration content is deleted as described by Dominic above.

Rudolf,

I eventually got around my problem by eliminating my need for the autoPtr variable. (I saved what information I needed from the object pointers as class variables.) I am sorry I am not much help, but I just want you to know that I just avoided the problem and therefore do not have a solution.

Good luck!

Dominic

rudolf.hellmuth January 14, 2013 11:05

It seems that when
Code:

autoPtr<T> = autoptr1 (new T...);
autoPtr<T> = autoptr2;

autoptr2(autoptr1);

the contents of autoptr1 are deleted.

Maybe when the pointer is deleted when passed to the new object. The information remains on the new object. When allocating a new list using an autoPtr, the first member gets the information and it is destroyed outside, so the second member gets nothing and an error occurs.

Reference:
http://www.cplusplus.com/reference/memory/auto_ptr/

rudolf.hellmuth January 14, 2013 11:18

Quote:

Originally Posted by djpiro (Post 401734)
Rudolf,

I eventually got around my problem by eliminating my need for the autoPtr variable. (I saved what information I needed from the object pointers as class variables.) I am sorry I am not much help, but I just want you to know that I just avoided the problem and therefore do not have a solution.

Good luck!

Dominic

Yes, Dominic, this would be an workaround. I would rather not have to do that, because I will have to gather the data outside the class, which is not very elegant. It would be much more elegant to do everything which concerns the object inside itself. :(

There must be another way. For now, I don't have time to figure this out and I will stick with this workaround as well.

Thanks Dominic!

Lieven February 1, 2013 14:18

Hi all,

Did anyone manage to address this issue in a 'clean' way? I'm having the same problem...

Cheers,

L


All times are GMT -4. The time now is 19:52.