Use FileStream intead of append file stream for M500, seems to fix crash
authorJim Morris <morris@wolfman.com>
Tue, 17 Dec 2013 01:57:58 +0000 (17:57 -0800)
committerJim Morris <morris@wolfman.com>
Tue, 17 Dec 2013 01:59:46 +0000 (17:59 -0800)
don't use use string in appendfilestream

src/libs/AppendFileStream.cpp
src/libs/AppendFileStream.h
src/libs/FileStream.h [new file with mode: 0644]
src/modules/communication/GcodeDispatch.cpp
src/modules/tools/extruder/Extruder.cpp

index 3e1bbb7..6e138b7 100644 (file)
@@ -1,9 +1,9 @@
 #include "AppendFileStream.h"
 #include "stdio.h"
 
-int AppendFileStream::puts(const char*str)
+int AppendFileStream::puts(const char *str)
 {
-    FILE *fd= fopen(this->fn.c_str(), "a");
+    FILE *fd= fopen(this->fn, "a");
     if(fd == NULL) return 0;
 
     int n= fwrite(str, 1, strlen(str), fd);
index 0b6daeb..ff36663 100644 (file)
@@ -1,16 +1,18 @@
 #ifndef _APPENDFILESTREAM_H_
 #define _APPENDFILESTREAM_H_
 
-#include <string>
 #include "StreamOutput.h"
+#include "string.h"
+#include "stdlib.h"
 
 class AppendFileStream : public StreamOutput {
     public:
-        AppendFileStream(const char *filename){ fn= filename; }
+        AppendFileStream(const char *filename) { fn= strdup(filename); }
+        virtual ~AppendFileStream(){ free(fn); }
         int puts(const char*);
 
     private:
-        std::string fn;
+        char *fn;
 };
 
 #endif
diff --git a/src/libs/FileStream.h b/src/libs/FileStream.h
new file mode 100644 (file)
index 0000000..b49d0a1
--- /dev/null
@@ -0,0 +1,18 @@
+#ifndef _FILESTREAM_H_
+#define _FILESTREAM_H_
+
+#include "StreamOutput.h"
+#include "stdlib.h"
+
+class FileStream : public StreamOutput {
+    public:
+        FileStream(const char *filename) { fd= fopen(filename, "a"); }
+        virtual ~FileStream(){ close(); }
+        int puts(const char *str){ return (fd == NULL) ? 0 : fwrite(str, 1, strlen(str), fd); }
+        void close() { if(fd != NULL) fclose(fd); fd= NULL; }
+
+    private:
+        FILE *fd;
+};
+
+#endif
index 47e2a2a..7931515 100644 (file)
@@ -15,7 +15,7 @@ using std::string;
 #include "modules/robot/Conveyor.h"
 #include "libs/SerialMessage.h"
 #include "libs/StreamOutput.h"
-#include "libs/AppendFileStream.h"
+#include "libs/FileStream.h"
 
 GcodeDispatch::GcodeDispatch() {}
 
@@ -123,8 +123,8 @@ void GcodeDispatch::on_console_line_received(void *line)
                             case 500: // M500 save volatile settings to config-override
                                 // delete the existing file
                                 remove(kernel->config_override_filename());
-                                // replace stream with one that appends to config-override file
-                                gcode->stream = new AppendFileStream(kernel->config_override_filename());
+                                // replace stream with one that writes to config-override file
+                                gcode->stream = new FileStream(kernel->config_override_filename());
                                 // dispatch the M500 here so we can free up the stream when done
                                 this->kernel->call_event(ON_GCODE_RECEIVED, gcode );
                                 delete gcode->stream;
index 3e864ef..3159733 100644 (file)
@@ -148,6 +148,7 @@ void Extruder::on_gcode_received(void *argument){
         }else if (gcode->m == 500 || gcode->m == 503){// M500 saves some volatile settings to config override file, M503 just prints the settings
             gcode->stream->printf(";E Steps per mm:\nM92 E%1.4f\n", this->steps_per_millimeter);
             gcode->mark_as_taken();
+            return;
         }
     }