2

I am creating an component, generalization of a template. For creation must be used string identifier.

I am replacing:

#define MYCOMPONENT_CONSTANT_IDENTIFIER "ID value" 

with

namespace myComponent { static const QString constant_identifier = "ID value" } 

to follow some codding standards (MISRA,...).

This should work regarding C++. And I checked it up at Constants-only header file C++.

This constant is defined in a header of a component "myComponent" and included in a header where my Indexer is initialized and component is created. This has not been changed at replacement time.

Replacement builds successfully, but fails on attempt to run. Segmentation fault hapends at:

template<> inline void TMyIndexer::Init() { Map(...) //before //Map( ENUM_VAL, QSharedPointer<ITableFieldDefs>(new myComponent::TTableFieldDefs(MYCOMPONENT_CONSTANT_IDENTIFIER)) ); Map( ENUM_VAL, QSharedPointer<ITableFieldDefs>(new myComponent::TTableFieldDefs(myComponent::constant_identifier)) ); Map(...) } 

Where:

// TStaticFieldDefs<> implements ITableFieldDefs typedef TStaticFieldDefs<myComponent::Fields> TTableFieldDefs; //constructor TStaticFieldDefs(QString id) : fId(id) {} 

If I go up the the stack:

2.) qstring.h: inline QString::QString(const QString &other) : d(other.d) { Q_ASSERT(&other != this); d->ref.ref(); }

1.) qatomic_x86_64.h: inline bool QBasicAtomicInt::ref()

I suppose there something wrong in template generalization, inline definition in the constructor or something else I am not aware.

Any explanation is welcome.

I am out of ideas and am kindly asking for help.

3
  • 2
    Have you tried a full rebuild? Maybe also by removing any temporary folder/file manually and then rebuilding? That fixed 90% of the segmentation faults I've had so far Commented Jul 26, 2013 at 11:39
  • Yes I did. Full clean up and rebuild.I have updated description. Commented Jul 26, 2013 at 12:10
  • I will try to check this up on an more trivial case. Tim thanx for responce. Commented Jul 26, 2013 at 13:29

1 Answer 1

2

My guess is that you're trying to use your constant object from a static context. A C++ standard states that order of static objects initialization is undefined. So you may reference uninitialized object that may cause a segfault.

Sign up to request clarification or add additional context in comments.

4 Comments

Good guess. That could be it. I doubt that constant is used from static context, but constant it self is a static and could be uninitialised. That explains also the segmentation fault on constant call. I will check it up and try to replace it with function as suggested at link answer.
Works. I replced static const QString constant_identifier = "ID value" with static const QString constant_identifier() {return "ID value";} And works good. Seems that inline definition and constant variable are not matching pair.
I'm glad it works. In my practice I've never seen inline definition and constant conflicts. Could you tell, how exactly do you call TMyIndexer::Init()?
You ware right and I was not deep enough. TMyIndexer::Init() is called over an static object in a singelton implementation. So your answer is perfect guess. Thanks for another question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.