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

autoPtr variable becomes invalid

Register Blogs Members List Search Today's Posts Mark Forums Read

Reply
 
LinkBack Thread Tools 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: 7
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: http://olesenm.github.io/
Posts: 777
Rep Power: 18
olesen will become famous soon enough
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: 7
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, 08:35
Default
  #4
New Member
 
Rudolf Hellmuth
Join Date: Sep 2012
Location: Galway, Ireland
Posts: 16
Rep Power: 4
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, 09:33
Default
  #5
New Member
 
Dominic Piro
Join Date: Dec 2009
Location: South Kingstown, RI
Posts: 11
Rep Power: 7
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, 11:05
Default
  #6
New Member
 
Rudolf Hellmuth
Join Date: Sep 2012
Location: Galway, Ireland
Posts: 16
Rep Power: 4
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, 11:18
Default
  #7
New Member
 
Rudolf Hellmuth
Join Date: Sep 2012
Location: Galway, Ireland
Posts: 16
Rep Power: 4
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, 14:18
Default
  #8
Senior Member
 
Lieven
Join Date: Dec 2011
Location: Mol, Belgium
Posts: 295
Rep Power: 13
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

Thread Tools
Display Modes

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 On
Pingbacks are On
Refbacks are On


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


All times are GMT -4. The time now is 11:04.