Static variable in Template Method Causes App Crash


#1

Hello:

I’m developing a new solution with M2M Studio version 1.0.2, I’m using a Fastrack for testing with ‘R74’ firmware.

The solution is implemented in C++, and it’s based on the HelloWorld sample.

Everything works well with the RTE environment, I can compile, debug and test the application, but when I compile and download the application for the ARM_ELF_GCC Release or Debug I’m getting this in the traces:

As you can see in the code below I’ve decided to implement a Singleton template (see listing ‘Singleton.h’) for the classes creation, the line
that causes the crash is:

static CBaseAvl obj;

If I remove the ‘static’ keyword it seems to work (at least it doesn’t crashes), but I do need the ‘static’ behaviour for that variable in that context, I don’t undestand why it works in the RTE and crashes with ARM_ELF.

I think I’m having some problems with the ‘arm-elf-g++.exe’ compilers.

Any idea on how to solve this issue???

Main.cpp:

#include "openat_os_interface.h"
#include "Singleton.h"
#include "CBaseAVL.h"

void MainTask ( void );

enum _mt_msgTasks_e
{
    MAIN_TASK_ID   // Main task
};

const adl_InitTasks_t adl_InitTasks [] =
{
{ MainTask, 3*4096, "MAIN", 3 },    // Main task
{ NULL, 0, NULL, 0 }
};


void MainTask ( void )
{
	CBaseAvl *objBaseAvl = NULL;

	TRACE (( 1, "Before: SINGLETONINSTANCE(CBaseAvl)" ));

	objBaseAvl = SINGLETONINSTANCE(CBaseAvl);
	objBaseAvl->Initialize();

	TRACE (( 1, "After: SINGLETONINSTANCE(CBaseAvl)" ));
}

Singleton.h:

#ifndef SINGLETON_H_
#define SINGLETON_H_

/*
	Template for Singleton implementation
*/
template <class T>
	class TSingleton
	{
	public:

		static T * Instance()
		{
			static T obj;

			return &obj;
		}

	private:
		TSingleton() {}
		~TSingleton() {}
		TSingleton(const TSingleton&) {}
		TSingleton& operator=(const TSingleton&) {}

	};

#define SINGLETON(T) protected:friend class TSingleton< T >;T(){};
#define SINGLETONINSTANCE(T) TSingleton< T >::Instance()

#endif /* SINGLETON_H_ */

CBaseAVL.h:

#ifndef CBASEAVL_H_
#define CBASEAVL_H_

#include "Singleton.h"

/*
	AVL Base Class
*/
class CBaseAvl 
{
	SINGLETON(CBaseAvl);

	public:
		bool Initialize(void);
		bool Finalize(void);
};

#endif /* CBASEAVL_H_ */

CBaseAVL.cpp:

#include "openat_os_interface.h"
#include "CBaseAvl.h"

/*
 *  Virtual Methods
 */

bool CBaseAvl::Initialize()
{
	static bool bTrue = true;
	TRACE (( 1, "bool CBaseAvl::Initialize()" ));
	return bTrue;
}

bool CBaseAvl::Finalize()
{
	TRACE (( 1, "bool CBaseAvl::Finalize()" ));
	return true;
}

openat_os_interface.h:

/*
 * openat_os_interface.h
 */

#ifndef OPENAT_OS_INTERFACE_H_
#define OPENAT_OS_INTERFACE_H_

#ifdef __cplusplus
extern "C"
{
#endif

#ifdef __cplusplus
#if __OAT_API_VERSION__ < 600
// For Open AT OS versions older than 6.00 (Open AT Software Suite 2.0), redefine types using more standard way
#include <stddef.h>
#include <stdint.h>
typedef uint8_t   u8;
typedef int8_t    s8;
typedef uint16_t  u16;
typedef int16_t   s16;
typedef uint32_t  u32;
typedef int32_t   s32;
typedef uint64_t  u64;
typedef int64_t   s64;
typedef char ascii;
#define  OK          0
#define  ERROR       -1

// Avoid old wm_types.h inclusion
#define __WM_TYPES_H__

#endif
#endif

// Include ADL interface
#include "adl_global.h"

#ifdef __cplusplus
}
#endif

#ifdef __cplusplus
// C++ only

// Define FALSE and TRUE macros if needed
// (depends on used Open AT OS version)
#ifndef FALSE
#define FALSE 0
#endif
#ifndef TRUE
#define TRUE  1
#endif

#endif

#endif /* OPENAT_OS_INTERFACE_H_ */