This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

sprintf() only works while debugging

I'm aware of the limitations on using "printf" in a non-debug environment, but apparently there must be something similar with sprintf. I'm using it to load a value into a brief string for transmission over BLE. This works perfectly while debugging - the value is sent using ble_nus_string_send() and is received by the Central as expected. However, when I disconnect the debugger and run in production mode, the notification works but the characteristic value comes through as a null string.

This does not appear to be causing any sort of error or fault - the connection is maintained, the other functions of the board work as expected.

Using SDK v12.3 on an nRF51822 custom board. Building with SES.

Anyone know how to fix this?

Parents
  • are you switching between Release and Debug configurations in SES?  If so it's likely that under the Release configuration sprintf () and printf() support are set to be removed to reduce code size.  Check the project configurations for this.  Also you can confirm that sprintf() is being included by looking at the .map file produced as part of the build for each configuration.

  • I imported the project from Keil and don't have separate Debug and Release configurations. (This is being built against v12.3.)

    I think I found the issue. I wasn't initializing the buffer so whatever junk was in the space allocated was being read as data and was causing the problem. I think I was just lucking out before. Removing all the printf statements change alignment and caused the buffer to be allocated on top of some garbage. As soon as I added memset(&buf, 0, 10); the problem cleared up in both debug and regular runtime mode. 

Reply
  • I imported the project from Keil and don't have separate Debug and Release configurations. (This is being built against v12.3.)

    I think I found the issue. I wasn't initializing the buffer so whatever junk was in the space allocated was being read as data and was causing the problem. I think I was just lucking out before. Removing all the printf statements change alignment and caused the buffer to be allocated on top of some garbage. As soon as I added memset(&buf, 0, 10); the problem cleared up in both debug and regular runtime mode. 

Children
Related