CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > Software User Forums > OpenFOAM > OpenFOAM Programming & Development

autoPtr variable becomes invalid

Register Blogs Community New Posts Updated Threads Search

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   May 2, 2011, 10:05
Default autoPtr variable becomes invalid
  #1
New Member
 
Dominic Piro
Join Date: Dec 2009
Location: South Kingstown, RI
Posts: 11
Rep Power: 16
djpiro is on a distinguished road
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
djpiro is offline   Reply With Quote

Old   May 3, 2011, 04:21
Default
  #2
Senior Member
 
Mark Olesen
Join Date: Mar 2009
Location: https://olesenm.github.io/
Posts: 1,684
Rep Power: 40
olesen has a spectacular aura aboutolesen has a spectacular aura about
Quote:
Originally Posted by djpiro View Post
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.
olesen is offline   Reply With Quote

Old   May 3, 2011, 09:27
Default
  #3
New Member
 
Dominic Piro
Join Date: Dec 2009
Location: South Kingstown, RI
Posts: 11
Rep Power: 16
djpiro is on a distinguished road
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
djpiro is offline   Reply With Quote

Old   January 14, 2013, 07:35
Default
  #4
Member
 
Rudolf Hellmuth
Join Date: Sep 2012
Location: Dundee, Scotland
Posts: 40
Rep Power: 13
rudolf.hellmuth is on a distinguished road
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.hellmuth is offline   Reply With Quote

Old   January 14, 2013, 08:33
Default
  #5
New Member
 
Dominic Piro
Join Date: Dec 2009
Location: South Kingstown, RI
Posts: 11
Rep Power: 16
djpiro is on a distinguished road
Quote:
Originally Posted by rudolf.hellmuth View Post
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
djpiro is offline   Reply With Quote

Old   January 14, 2013, 10:05
Default
  #6
Member
 
Rudolf Hellmuth
Join Date: Sep 2012
Location: Dundee, Scotland
Posts: 40
Rep Power: 13
rudolf.hellmuth is on a distinguished road
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 is offline   Reply With Quote

Old   January 14, 2013, 10:18
Default
  #7
Member
 
Rudolf Hellmuth
Join Date: Sep 2012
Location: Dundee, Scotland
Posts: 40
Rep Power: 13
rudolf.hellmuth is on a distinguished road
Quote:
Originally Posted by djpiro View Post
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!
rudolf.hellmuth is offline   Reply With Quote

Old   February 1, 2013, 13:18
Default
  #8
Senior Member
 
Lieven
Join Date: Dec 2011
Location: Leuven, Belgium
Posts: 299
Rep Power: 22
Lieven will become famous soon enough
Hi all,

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

Cheers,

L
Lieven is offline   Reply With Quote

Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
[swak4Foam] GroovyBC the dynamic cousin of funkySetFields that lives on the suburb of the mesh gschaider OpenFOAM Community Contributions 300 October 29, 2014 18:00
emag beta feature: charge density charlotte CFX 4 March 22, 2011 09:14
error in COMSOL:'ERROR:6164 Duplicate Variable' bhushas COMSOL 1 May 30, 2008 04:35
Phase locked average in run time panara OpenFOAM 2 February 20, 2008 14:37
Replace periodic by inlet-outlet pair lego CFX 3 November 5, 2002 20:09


All times are GMT -4. The time now is 18:54.