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.

Community
  • 1
  • 1
urkon
  • 233
  • 1
  • 5
  • 15
  • 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 – Tim Meyer Jul 26 '13 at 11:39
  • Yes I did. Full clean up and rebuild.I have updated description. – urkon Jul 26 '13 at 12:10
  • I will try to check this up on an more trivial case. Tim thanx for responce. – urkon Jul 26 '13 at 13:29

1 Answers1

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.

Evgeny Shavlyugin
  • 421
  • 1
  • 4
  • 9
  • 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](http://stackoverflow.com/questions/1005685/c-static-initialization-order) answer. – urkon Jul 27 '13 at 00:30
  • 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. – urkon Jul 29 '13 at 05:21
  • 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()? – Evgeny Shavlyugin Jul 29 '13 at 05:57
  • 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. – urkon Jul 29 '13 at 08:33