usbx icon indicating copy to clipboard operation
usbx copied to clipboard

Null pointer exception in the thread named "_ux_device_class_storage_thread"

Open HeBea opened this issue 1 year ago • 6 comments

Describe the bug I run MSC class on my device successfully but with a very low probability of null pointer exceptions which lcated in _ux_device_class_storage_thread/_ux_device_class_storage_read/_ux_device_class_storage_csw_send/_ux_utility_memory_copy. There is a loop in the thread, it runs all the time. I think that if ux_slave_transfer_request_data_pointer is set to NULL by CONFIGURA or otherwise,the loop will crash when do memory copy at this moment. It is just my thought. Could you please help on this inssue? How to avoid null pointer exception?

HeBea avatar Mar 22 '23 07:03 HeBea

Did anyone have the same experience?

HeBea avatar Mar 22 '23 09:03 HeBea

I did not see such exceptions on my side.

ux_slave_transfer_request_data_pointer is currently allocated in ux_device_stack_initialize, which is called before device class storage class registration and storage thread creation. It should be always available before device stack uninitialized.

xiaocq2001 avatar Mar 24 '23 02:03 xiaocq2001

Thank you for your reply. There is a difference in my side. We free the memory of non-control endpoint and set ux_slave_transfer_request_data_pointer to NULL in ux_slave_dcd_function when UX_DCD_DESTROY_ENDPOINT event comes. It may cause the loop to get a null pointer. I see that the demo code just set ux_sim_slave_ed_status to unused in the ux_slave_dcd_function. Should I do as the demo code? Do you have a better suggestion?

HeBea avatar Mar 27 '23 06:03 HeBea

Yes, please refer to demo code. Let stack manage the memory.

xiaocq2001 avatar Mar 27 '23 07:03 xiaocq2001

Shoul I set ux_slave_transfer_request_data_pointer to NULL when free the memory? I am not sure about this. If do not set it to NULL, the thread will write to the memory which is freed.

HeBea avatar Mar 28 '23 04:03 HeBea

The thread is created (in class initialization invoked by class registration) after buffer allocated (in stack initialization), and the buffer is not freed (in stack uninitialization) before thread deletion (in class uninitialization invoked by class unregistration).

xiaocq2001 avatar Mar 29 '23 02:03 xiaocq2001