witnessmenow/spotify-api-arduino

Memory Leaks

TheNitek opened this issue · 2 comments

The current version is exposing internal memory areas of the ArduionJSON as part of the return values which might lead to "funny" behaviour.

I created a minial example show the issue:

#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <ArduinoSpotify.h>
#include <ArduinoJson.h>

char ssid[] = "aa";
char password[] = "aa";

char clientId[] = "aaa";
char clientSecret[] = "aaa";

#define SPOTIFY_MARKET "DE"
#define SPOTIFY_REFRESH_TOKEN "aaa"

#include <ArduinoSpotifyCert.h>

WiFiClientSecure client;
ArduinoSpotify spotify(client, clientId, clientSecret, SPOTIFY_REFRESH_TOKEN);

void setup() {
    Serial.begin(115200);

    WiFi.mode(WIFI_STA);
    WiFi.begin(ssid, password);

    while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
    }
    Serial.println();

    client.setCACert(spotify_server_cert);

    if(!spotify.refreshAccessToken()){
        Serial.println("Failed to get access tokens");
    }

    CurrentlyPlaying currentlyPlaying = (spotify.getCurrentlyPlaying(SPOTIFY_MARKET));

    while(1) {
        Serial.println(currentlyPlaying.firstArtistName);
        delay(1000);
        spotify.getCurrentlyPlaying(SPOTIFY_MARKET);
        yield();
    }
}

void loop() {
}

This should print the same artist name (the one that was running when the programm started) every second, but with the current version the output usually turns into rubish after the first iteration. Issue is address by #15 (at the cost of high stack usage)

If this line is a copy CurrentlyPlaying currentlyPlaying = (spotify.getCurrentlyPlaying(SPOTIFY_MARKET)); then this will work as expected when I merge #21 , but I assume its just a pointer that is updated , which means this will still not work as you describe, you would need to memcpy to do what you want.

All changes related to this have been merged to the main branch.