Ankündigung

Einklappen
Keine Ankündigung bisher.

memory not valid

Einklappen
X
  • Filter
  • Zeit
  • Anzeigen
Alles löschen
neue Beiträge

  • memory not valid

    Hello, I made a program which runs fine for one or two times (after a reboot of miniserver), but then gets a memory not valid error. Does anybody see why?

    char *l;
    while (l==l)
    {
    char *value;
    char *result = httpget("api.wunderground.com","/api/apikey/alerts/conditions/forecast/hourly/long%3ADL/pws%3A1/bestfct%3A1/q/gent,belgium.json");
    value = strstrskip(result, "city":"");
    char *substringg = calloc(1, 15);
    char *newsubstring= calloc(1, 150);
    strncpy(substringg,value,1);
    char *breakout = """;
    while (strcmp(substringg, breakout) != 0)
    {
    //printf("substringresultn = %s", substringg );
    strcat(newsubstring, substringg);
    //printf("newsubstringresultn = %s", newsubstring );
    value = strstrskip(value,substringg);
    free(substringg);
    strncpy(substringg,value,1);
    }
    setoutputtext(0, newsubstring);
    free(value);
    free(substringg);
    free(result);
    free(newsubstring);
    free(breakout);
    sleeps(10);
    printf("weer opgehaald");
    }

    Thanks.

  • #2
    There is a ready solution for querying the Wunderground weather data in this thread:
    https://www.loxforum.com/forum/faqs-...stein-abfragen
    PN/PM: Ich bevorzuge die Beantwortung von Fragen in öffentlichen Threads, wo andere mithelfen und mitprofitieren können. Herzlichen Dank!

    Kommentar


    • #3
      Yep, I saw that one, thanks, but it wasn't the service as such that was important for me, it was to learn the program block/code. It could have been any other service. I wonder why it works 2 times and then raises an error. When I reboot the miniserver it works again for 2 times.

      Kommentar


      • #4
        Hi Prince,
        I am not sure, because I do not use pico c programs, but what I see is, that you allocate "char *result" but you never free it later on. So maybe there is a buffer overflow.
        I don´t know if the server has any garbage collector or automatic free for memory.
        Try free(result) in the last slope before "sleep"
        Haus: Bj 1959, gekauft 2011, totale Entkernung, Dachausbau, Erweiterung & Vergrößerung: Start: 2014, Ende: 2050
        Loxone: 1x MS, 5 x Ext., 3 x Relay Ext., 1 x Dimmer Ext., 2 x 1-wire Ext., 1 x DMX Ext. 1 x TREE Ext. mehr kommt noch
        Licht: DMX LED Beleuchtung (24V), MW HLG Serie und eldoled Dimmer
        Heizung: Brötje WBS 22F, OG Heizkörper und FuBoHeizung über RTL, EG FuBoHeizung

        Kommentar


        • Prince1001
          Prince1001 kommentierte
          Kommentar bearbeiten
          Hi Simon, thanks for the feedback, but I free "result" (between free(substringg); and free(newsubstring). I tried to move it to just before "sleep" but same result.

      • #5
        there is a Free(result)

        i think the Problem is:

        free(substringg);
        strncpy(substringg,value,1);

        Kommentar


        • Prince1001
          Prince1001 kommentierte
          Kommentar bearbeiten
          Thanks Robert, I'll have a look at this.

      • #6
        My C skills seem to be also far away, but it might be a problem that you redefine variables on every loop (char *value; char *result ...)
        Possibly define the variables (pointers) outside the loop, and address them as normal variables/pointers inside.
        PN/PM: Ich bevorzuge die Beantwortung von Fragen in öffentlichen Threads, wo andere mithelfen und mitprofitieren können. Herzlichen Dank!

        Kommentar


        • Christian Fenzl
          Christian Fenzl kommentierte
          Kommentar bearbeiten
          Nope, this is not the problem. Also C can handle this.

        • Robert L.
          Robert L. kommentierte
          Kommentar bearbeiten
          wolltest du mir antworten?

          http://www.cplusplus.com/reference/cstring/strncpy/

          i still think Free the "substringg" is the problem
          strncpy expects a string that is at least num (+1?) long..

        • Christian Fenzl
          Christian Fenzl kommentierte
          Kommentar bearbeiten
          Robert L. Nein, ich hab mich selbst korrigiert, das Wieder-initialisieren einer Variable in einer Schleife funktioniert auch in C.

      • #7
        Code:
        char *l;
        while (l==l)
        of course is a strange construct, why not simply (1==1)?
        But it won't fix the error.
        PN/PM: Ich bevorzuge die Beantwortung von Fragen in öffentlichen Threads, wo andere mithelfen und mitprofitieren können. Herzlichen Dank!

        Kommentar


        • #8
          Another issue (that I don't know if PicoC handles different, but C++/C# needs) is escaping of quotes:
          Code:
          value = strstrskip(result, "city":"");
          --> value = strstrskip(result, "city\":\"");
          
          char *breakout = """;
          --> char *breakout = "\"";
          PN/PM: Ich bevorzuge die Beantwortung von Fragen in öffentlichen Threads, wo andere mithelfen und mitprofitieren können. Herzlichen Dank!

          Kommentar


          • Prince1001
            Prince1001 kommentierte
            Kommentar bearbeiten
            Hi Christian, I must have been lost in the copy but the quotes are escaped indeed

        • #9
          I deleted all the free(); things and now it works every time... (except free(result1))
          Finally I added a push button to it and this version has no memory issue.(copied from the example Christian pasted)

          Code:
          int nEvents;
          float fEingangEins;
          char *host="api.wunderground.com";
          char *page="/api/xx/alerts/conditions/forecast/hourly/long%3ADL/pws%3A1/bestfct%3A1/q/gent,belgium.json";
          char *breakout = "\"";
          while(1==1){
              nEvents = getinputevent();  //Eingänge auslesen.
              if (nEvents & 0x18){               // Bitmaske für die Eingang.
                  fEingangEins = getinput(0); // Eingänge auf Veränderung prüfen, und ggf. Variabeln aktualisieren.
              }
              if (fEingangEins != 0){
                  setoutputtext(0, "ophalen gemeente..");
                  fEingangEins = 0; // 0 setzten, damit der Abruf pro Flanke nur einmal erfolgt.    
                  char *value;
                  char *result1 = httpget(host,page);
                  printf("t");
                  value = strstrskip(result1, "city\":\"");
                  free(result1);
                  char *substringg = calloc(1, 15);
                  char *newsubstring= calloc(1, 15);
                  strncpy(substringg,value,1);
                  while (strcmp(substringg, breakout) != 0) 
                      {
                          printf("substringresultn = %s", substringg );
                          strcat(newsubstring, substringg);
                          //printf("newsubstringresultn = %s", newsubstring );
                          value = strstrskip(value,substringg);
                          strncpy(substringg,value,1);
                      }
                  setoutputtext(0, newsubstring);    
                  printf("weer opgehaald");
                  sleeps(10);
              }
          }

          Kommentar

          Lädt...
          X