Pages: 1 [2]   Go Down
Author Topic: Serial.print incomplete string ?  (Read 1168 times)
0 Members and 1 Guest are viewing this topic.
Queens, New York
Offline Offline
Faraday Member
**
Karma: 84
Posts: 3417
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

How would you know if you are having memory leakage problems, how do I test this?

Because I use strings a lot but, I never noticed any problems.
Logged

Created Libraries:
NPV2 (NewPasswordV2),  TFT_Extension, OneWireKeypad, SerialServo.
Will provide libraries if asked in PM or forum.

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 72
Posts: 7171
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Fellows, I think you may have the String thing mixed up. The OP is only using String in actionscript. Isn't that run on a computer instead of on Arduino?  smiley-cool

Regarding memory leak, I heard from experts that is a problem with free(). HazardsMind, you may not have experienced a problem possibly because you are not defining String in subroutines or loop (did you just define String outside functions?). There are plenty of others that did get into trouble and fixed the problem by not using String. The problem is if you have a String in functionX then every time the function quits free() should clean up the String but it doesn't, that's what I heard.
Logged


Queens, New York
Offline Offline
Faraday Member
**
Karma: 84
Posts: 3417
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@liurd

I'll post a sample from my robot code when I get home, I don't have it on me right now.

It there a way to see the memory leakage?
Logged

Created Libraries:
NPV2 (NewPasswordV2),  TFT_Extension, OneWireKeypad, SerialServo.
Will provide libraries if asked in PM or forum.

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 72
Posts: 7171
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

It there a way to see the memory leakage?

I used this:

http://playground.arduino.cc/Code/AvailableMemory
Logged


Queens, New York
Offline Offline
Faraday Member
**
Karma: 84
Posts: 3417
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This also pertains to String objects too, right?
Logged

Created Libraries:
NPV2 (NewPasswordV2),  TFT_Extension, OneWireKeypad, SerialServo.
Will provide libraries if asked in PM or forum.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
This also pertains to String objects too, right?
What does? Memory is memory.
Logged

Queens, New York
Offline Offline
Faraday Member
**
Karma: 84
Posts: 3417
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The memory leakage, their are two types of strings, character arrays and objects. Do they both have the same problem?
Logged

Created Libraries:
NPV2 (NewPasswordV2),  TFT_Extension, OneWireKeypad, SerialServo.
Will provide libraries if asked in PM or forum.

UK
Offline Offline
Shannon Member
****
Karma: 222
Posts: 12541
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The memory leakage, their are two types of strings, character arrays and objects. Do they both have the same problem?

The problem is associated with the release of dynamically allocated memory.

String objects use dynamically allocated memory, so they inherently suffer from the problem.

'C' strings (null-terminated char arrays) don't use dynamically allocated memory so don't inherently suffer from the problem.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

0
Offline Offline
Tesla Member
***
Karma: 141
Posts: 9470
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
The problem is associated with the release of dynamically allocated memory.

The real problem in the forum is that 9 out of 10 instances (and I'm being liberal) where the forum hens cackle about not using Strings, Strings is never been proven to be the actual issue causing the problem with the code.
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Queens, New York
Offline Offline
Faraday Member
**
Karma: 84
Posts: 3417
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@liurd

Here is my robot code, the only time I had a problem was when I tried to clear multiple strings like this, X,Y,State = ""
I got garbage when I tried to send the data. That was the only problem I had, everything works now.

Here:
Code:
/*
control test
 */
#include<string.h>
int DRV1,DRV2,STRR,STRL;
int x = 0;
int y = 0;
int s = 0;
void move(int x = 0,int y = 0,int s = 0);
//double z;
String val = "";
String  X= "";
String  Y= "";
String  state = "";
//int state = 0;
int currentCommand = 0;

int ledpin = 13;
byte Mopen = 4;
byte Mclosed = 2;
byte M1L = 3;// PWM
byte M2L = 5;// PWM
byte M1R = 9;// PWM
byte M2R = 6;// PWM

void setup()
{
  pinMode(ledpin, OUTPUT);  // pin 13 (on-board LED) as OUTPUT
  pinMode(Mopen, OUTPUT);                               
  pinMode(Mclosed, OUTPUT);
  pinMode(M1L, OUTPUT);                               
  pinMode(M1R, OUTPUT);
  pinMode(M2L, OUTPUT);                               
  pinMode(M2R, OUTPUT);
  Serial.begin(9600);       // start serial communication at 115200bps

}

void loop() {
  if( Serial.available())       // if data is available to read
  {
    digitalWrite(ledpin, HIGH);
    char c= Serial.read();
    if (c == ','){
      currentCommand++;
    }
    else {
      val += c;
      //Serial.println(val);
      switch (currentCommand) {

      case 0:
        X += val;
        val = "";
        break;
 
      case 1:
        Y += val;
        val = "";
        break;

      case 2:
      //Serial.println("X: "+X);
      //Serial.println("Y: "+Y);   
        state = val;
        currentCommand = 0;
        val = "";
        //Serial.println("state: "+state);
        //Serial.println();
        x=X.toInt();
        y=Y.toInt();
        s=state.toInt();
        X=""; Y=""; state="";
        move(x, y, s);
        break;
      }
    }
  }
}
   
  void move(int x, int y, int s)
  { 
      x=constrain(x, -90, 90);
      y=constrain(y, -90, 90);

  //Movement varibles
 
  int DRV2 = map(x, -90, 0, 255, 0);
  int DRV1 = map(x, 0, 90, 0, 255);
  int STRR = map(y, -90, 0, 255, 0);
  int STRL = map(y, 0, 90, 0, 255); 

  if(x > 0)//forwards               
  {
    //Serial.println("Driving");
    analogWrite(M1L, abs(DRV1 - STRL)); analogWrite(M1R, abs(DRV1 - STRR));   
    analogWrite(M2L, 0); analogWrite(M2R, 0);   
  }
  else if(x < 0)//backwards               
  {
    //Serial.println("Driving");
    analogWrite(M1L, 0); analogWrite(M1R, 0);   
    analogWrite(M2L, abs(DRV2 - STRR)); analogWrite(M2R, abs(DRV2 - STRL));   
  }
  else if(x == 0 && y >0)//right             
  {
    //Serial.println("Driving");
    analogWrite(M1L, STRL); analogWrite(M1R, 0);   
    analogWrite(M2L, 0); analogWrite(M2R, STRL);   
  }
  else if(x == 0 && y < 0)//left             
  {
    //Serial.println("Driving");
    analogWrite(M1L, 0); analogWrite(M1R, STRR);   
    analogWrite(M2L, STRR); analogWrite(M2R, 0);   
  }

  else //full stop
  {
    digitalWrite(M1L, LOW); digitalWrite(M1R, LOW);       
    digitalWrite(M2L, LOW); digitalWrite(M2R, LOW);   
  }

   if(s == 1)
    {
      //Serial.println("Claw Opening");
      digitalWrite(Mopen, HIGH); digitalWrite(Mclosed, LOW);
    }
    if(s == 2)
    {
      //Serial.println("Claw Closing");
      digitalWrite(Mopen, LOW); digitalWrite(Mclosed, HIGH);
    }
    if(s == 0)
    {
      digitalWrite(Mopen, LOW);  digitalWrite(Mclosed, LOW);
    }
   else{
    digitalWrite(ledpin, LOW);
    x=0; y=0; s=0;
  }
 }

I tried to use arrays for the X,Y and state but it wouldn't work correctly, so I went back to strings and it worked.
« Last Edit: December 14, 2012, 10:31:31 pm by HazardsMind » Logged

Created Libraries:
NPV2 (NewPasswordV2),  TFT_Extension, OneWireKeypad, SerialServo.
Will provide libraries if asked in PM or forum.

UK
Offline Offline
Shannon Member
****
Karma: 222
Posts: 12541
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
The problem is associated with the release of dynamically allocated memory.

The real problem in the forum is that 9 out of 10 instances (and I'm being liberal) where the forum hens cackle about not using Strings, Strings is never been proven to be the actual issue causing the problem with the code.

I don't know where your figures come from but, regardless of what the true figures are, when somebody using Strings reports problems of the sort that can be caused by the known problem in Strings, it makes sense to remove that as a possible cause before you spend any more time looking for other possible causes. I assume the String class was introduced to simplify text processing, but using 'c' strings directly is not especially difficult so it is (imo) reasonable to recommend this as a speculative fix even when the cause is not known.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

0
Offline Offline
Tesla Member
***
Karma: 141
Posts: 9470
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
when somebody using Strings reports problems of the sort that can be caused by the known problem in Strings, it makes sense to remove that as a possible cause before you spend any more time looking for other possible causes.

I've read the often posted two discussions about what is broken with the String function. It appears that the clearing of a memory space does not happen when String is used in sub functions in rather complex code. I have yet to see an issue corrected in the typical simple code (or any code posted in the forum as I remember)  by not using the String class.

Quote
I assume the String class was introduced to simplify text processing, but using 'c' strings directly is not especially difficult so it is (imo) reasonable to recommend this as a speculative fix even when the cause is not known.

The String class does simplify using strings for the new forum members. The forum String class hens cackle about not using Strings when a newbie uses them in code, but the hens almost never help the newbie by correcting the posted code to not use strings. Using 'c' is complex and difficult, especially for the newbie, and apparently for some of the hens as they never demonstrate coorecting code issues by not using Strings. The main reason  the hens don't correct the code is that Strings is not actually causing the problem with the code. Would make the hens look somewhat clueless when not using Strings does not fix the problem.  smiley-roll
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

UK
Offline Offline
Shannon Member
****
Karma: 222
Posts: 12541
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That seems to be pure speculation on your part about the abilities and motivation of forum contributors. If you don't believe the String class is vulnerable to the memory leak problem then good for you, but I think you're in a minority.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

0
Offline Offline
Tesla Member
***
Karma: 141
Posts: 9470
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
If you don't believe the String class is vulnerable to the memory leak problem then good for you, but I think you're in a minority.

Never have I ever sad that I don't beleve the String class has a memory leak. Generally microtrollers work the way they are wired and code runs the way it is written. The problem I have is that certain forum persons inject into code problem solving String class issues that do not have any relation to solving the actual issues at hand. Persons who do this, in my opinion, are displaying a technical difficiency they have by not being able to stay on focus to solve the actual code problem. Most of the time they could just as well suggest sacrificing a 20 piece bucket of KFC extra crispy chicken to some diety, as the result would be the same.



Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Pages: 1 [2]   Go Up
Jump to: