|
[Sponsors] |
May 2, 2011, 10:05 |
autoPtr variable becomes invalid
|
#1 |
New Member
Dominic Piro
Join Date: Dec 2009
Location: South Kingstown, RI
Posts: 11
Rep Power: 16 |
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 |
|
May 3, 2011, 04:21 |
|
#2 |
Senior Member
Mark Olesen
Join Date: Mar 2009
Location: https://olesenm.github.io/
Posts: 1,685
Rep Power: 40 |
Why not use a PtrList<BaseClass> for storage instead? I think this might avoid many of your problems.
|
|
May 3, 2011, 09:27 |
|
#3 |
New Member
Dominic Piro
Join Date: Dec 2009
Location: South Kingstown, RI
Posts: 11
Rep Power: 16 |
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 |
|
January 14, 2013, 07:35 |
|
#4 |
Member
Rudolf Hellmuth
Join Date: Sep 2012
Location: Dundee, Scotland
Posts: 40
Rep Power: 13 |
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; Code:
autoPtr<volScalarField> C 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. |
|
January 14, 2013, 08:33 |
|
#5 | |
New Member
Dominic Piro
Join Date: Dec 2009
Location: South Kingstown, RI
Posts: 11
Rep Power: 16 |
Quote:
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 |
||
January 14, 2013, 10:05 |
|
#6 |
Member
Rudolf Hellmuth
Join Date: Sep 2012
Location: Dundee, Scotland
Posts: 40
Rep Power: 13 |
It seems that when
Code:
autoPtr<T> = autoptr1 (new T...); autoPtr<T> = autoptr2; autoptr2(autoptr1); 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/ |
|
January 14, 2013, 10:18 |
|
#7 | |
Member
Rudolf Hellmuth
Join Date: Sep 2012
Location: Dundee, Scotland
Posts: 40
Rep Power: 13 |
Quote:
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! |
||
February 1, 2013, 13:18 |
|
#8 |
Senior Member
Lieven
Join Date: Dec 2011
Location: Leuven, Belgium
Posts: 299
Rep Power: 22 |
Hi all,
Did anyone manage to address this issue in a 'clean' way? I'm having the same problem... Cheers, L |
|
|
|
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 |