On 28.6.2013. 20:20, Ghyslain Leclerc wrote:
First post here. Not a programmer by trade, but a medical physicist, so my apologies if my question seems too much of a newbie question. And hopefully, my programing jargon won’t be too bad.
Your post is clearer than most.
In either case (pointer or object), my actual problem comes when I want to add options to a options_description. I can easily use the add function : myOptionsDescriptionObject.add( theOtherOptionsDescriptionObject );
but that adds empty lines in the output format and creates artificial grouping of options, which I would like to avoid if possible. That being said, given the current state of our code, I will implement this version now as this is the "best" solution that works.
There is an 'add' overload which accepts shared_ptr. This one does not add grouping. If you make your base class contain shared pointers I think you could avoid this issue. Alternatively - you can create a new shared_ptr on demand: shared_ptr< option_description > tmp( new option_description( theOtherOptionsDescriptionObject ) ); myOptionsDescriptionObject.add( tmp );
The way I am trying to get around that is to extract for each option_description contained in the options_description object the name, the description and the value_semantic and then use these three extracted values with the add_options() function like so: muOptionsDescriptionObject.add_options()( other.options()->name(), other.options()->semantic().get(), other.options()->description() );
Works perfectly for name and description, but fails for value_semantic. It compiles, but generates a "The program has unexpectedly finished." message. My hypothesis for what is happening is that I am using the .get() to extract the raw pointer from the shared_ptr returned, but the shared_ptr somehow deletes the memory and my pointer becomes invalid.
I don't think that is what happens. As far as I can see - description object tries to take ownership of the value semantic object. If you pass value which is owned by a shared_ptr you get double delete crash. Please note that I am not familiar with program_options, most of what I wrote is based on a quick program_options code review, so I could be wrong. Anyway, I hope this helps.