Navigation Menu

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

For ESP32, initialization of static object during declaration with derived class type doesn't work. #46335

Closed
vermaabhishek-eaton opened this issue Jun 7, 2022 · 7 comments
Assignees
Labels
area: C++ bug The issue is a bug, or the PR is fixing a bug platform: ESP32 Espressif ESP32 priority: low Low impact/importance bug

Comments

@vermaabhishek-eaton
Copy link

vermaabhishek-eaton commented Jun 7, 2022

  • For esp32 board, if we are initializing static object with derived class type during declaration, it doesn't work.
  • Same code is working with nucleo_f429zi board.
  • Refer below example code to reproduce the issue.

1) Header file content Test_Code.h

/* Parent class- test1 */
class test1
{
	public:
		virtual void Available( void );
};

/*  Derived class- test2 */
class test2 : public test1
{
	public:
		test2();
		~test2();
		void Available( void );
};

/* Standalone class- xyz */
class xyz
{
	public:
		xyz();
		~xyz();
		static void Allocate(void);
	private:
		static test2 m_debug_obj;
		static test1* m_abc_ptr;
};

2) Source file content Test_Code.cpp

#include <zephyr.h>
#include "Test_Code.h"

test2 xyz::m_debug_obj;
test1* xyz::m_abc_ptr = &m_debug_obj;  // this initialization doesn't work for ESP32 target.

/* Derived Class function*/
test2::test2()
{}

test2::~test2()
{}

void test2::Available( void )
{
	unsigned char a = 0;
	printk("m_abc_ptr initialized correctly\n");
	a++;

}

/* Static function of Class xyz */
void xyz::Allocate( void )
{
	static unsigned char temp = 0;
/* If we remove initialization during declaration and enable below line then initialization works */
//	m_abc_ptr = (&m_debug_obj);
	if ( m_abc_ptr != nullptr )
		{
			m_abc_ptr->Available();
			temp ++;
		}
}

/* Main function */
void main( void )
{
	xyz::Allocate();
	while ( 1 )
	{
		printk( "In loop\n" );
		k_msleep( 1000 );
	}
}
  • Use following config option in prj.conf
    CONFIG_CPLUSPLUS=y
    CONFIG_MINIMAL_LIBC=y
    CONFIG_MINIMAL_LIBC_MALLOC=y
    CONFIG_MINIMAL_LIBC_MALLOC_ARENA_SIZE=16384
    CONFIG_THREAD_NAME=y
    CONFIG_SYS_CLOCK_TICKS_PER_SEC=1000

To Reproduce
1. Build Test_Code.cpp with Zephyr v3.0.0 for nucleo_f429zi and esp32 board.
2. Connect putty with configuration ( 115200 baudrate, 8bit, Parity-none, 1Stop bit ) and observe output logs.
3. For nucleo_f429zi, you will get "m_abc_ptr initialized correctly" log.
4. For esp32, you will get Backtrace logs.

  • As a workaround for this issue we are initializing m_abc_ptr object through function call.
@vermaabhishek-eaton vermaabhishek-eaton added the bug The issue is a bug, or the PR is fixing a bug label Jun 7, 2022
@stephanosio
Copy link
Member

@vermaabhishek-eaton which toolchain and version are you using?

@sylvioalves does this ring a bell?

@sylvioalves
Copy link
Collaborator

@vermaabhishek-eaton Would you retry using v3.1.0? Please, note that ESP32 toolchain now uses Zephyr-SDK. Check this again and make sure export ZEPHYR_TOOLCHAIN_VARIANT="zephyr" is properly set.

@vermaabhishek-eaton
Copy link
Author

@vermaabhishek-eaton which toolchain and version are you using?

@sylvioalves does this ring a bell?

espressif toolchain I have installed through "west espressif install" command. Its version is esp-2020r3-8.4.0

@vermaabhishek-eaton
Copy link
Author

@vermaabhishek-eaton Would you retry using v3.1.0? Please, note that ESP32 toolchain now uses Zephyr-SDK. Check this again and make sure export ZEPHYR_TOOLCHAIN_VARIANT="zephyr" is properly set.

Yes I can try on it.

@stephanosio
Copy link
Member

Assigning this issue to @sylvioalves since this is an Espressif toolchain issue.

Please try with Zephyr 3.1.0 using the Zephyr SDK. If this is still an issue with the Zephyr SDK, you can assign it back to me.

@fabiobaltieri fabiobaltieri added the priority: low Low impact/importance bug label Jun 15, 2022
@sylvioalves
Copy link
Collaborator

@vermaabhishek-eaton, were you able to check this?

@vermaabhishek-eaton
Copy link
Author

@vermaabhishek-eaton, were you able to check this?

yes, it is working with zephyr v_3.1.0 using zephyr sdk.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: C++ bug The issue is a bug, or the PR is fixing a bug platform: ESP32 Espressif ESP32 priority: low Low impact/importance bug
Projects
None yet
Development

No branches or pull requests

5 participants