mirror of
https://github.com/krgamestudios/Toy.git
synced 2026-04-15 14:54:07 +10:00
Fixed memory leak
This commit is contained in:
@@ -3,7 +3,9 @@
|
|||||||
#include "drive_system.h"
|
#include "drive_system.h"
|
||||||
|
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <errno.h>
|
||||||
|
|
||||||
typedef struct Toy_File
|
typedef struct Toy_File
|
||||||
{
|
{
|
||||||
@@ -68,8 +70,6 @@ static int nativeOpen(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments)
|
|||||||
|
|
||||||
const char* mode = Toy_toCString(TOY_AS_STRING(modeLiteral));
|
const char* mode = Toy_toCString(TOY_AS_STRING(modeLiteral));
|
||||||
|
|
||||||
interpreter->printOutput(filePath);
|
|
||||||
|
|
||||||
// build file object
|
// build file object
|
||||||
Toy_File* file = TOY_ALLOCATE(Toy_File, 1);
|
Toy_File* file = TOY_ALLOCATE(Toy_File, 1);
|
||||||
file->error = 0;
|
file->error = 0;
|
||||||
@@ -79,6 +79,9 @@ static int nativeOpen(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments)
|
|||||||
// attempt to open file
|
// attempt to open file
|
||||||
file->fp = fopen(filePath, mode);
|
file->fp = fopen(filePath, mode);
|
||||||
if (file->fp == NULL) {
|
if (file->fp == NULL) {
|
||||||
|
TOY_FREE(Toy_File, file);
|
||||||
|
fprintf(stderr, "Error code: %d\n", errno);
|
||||||
|
fprintf(stderr, "File not found: %s\n", filePath);
|
||||||
file->error = 1;
|
file->error = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -107,28 +110,37 @@ static int nativeOpen(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments)
|
|||||||
}
|
}
|
||||||
|
|
||||||
static int nativeClose(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments) {
|
static int nativeClose(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments) {
|
||||||
// if (arguments->count != 1) {
|
if (arguments->count != 1) {
|
||||||
// interpreter->errorOutput("Incorrect number of arguments to open\n");
|
interpreter->errorOutput("Incorrect number of arguments to close\n");
|
||||||
// return -1;
|
return -1;
|
||||||
// }
|
}
|
||||||
|
|
||||||
// Toy_Literal fileLiteral = Toy_popLiteralArray(arguments);
|
Toy_Literal selfLiteral = Toy_popLiteralArray(arguments);
|
||||||
|
|
||||||
// // parse the file (if it's an identifier)
|
// parse the self (if it's an identifier)
|
||||||
// Toy_Literal fileLiteralIdn = fileLiteral;
|
Toy_Literal selfLiteralIdn = selfLiteral;
|
||||||
// if (TOY_IS_IDENTIFIER(fileLiteral) && Toy_parseIdentifierToValue(interpreter, &fileLiteral)) {
|
if (TOY_IS_IDENTIFIER(selfLiteral) && Toy_parseIdentifierToValue(interpreter, &selfLiteral)) {
|
||||||
// Toy_freeLiteral(fileLiteralIdn);
|
Toy_freeLiteral(selfLiteralIdn);
|
||||||
// }
|
}
|
||||||
|
|
||||||
// // check the file type
|
// check self type
|
||||||
// if (!TOY_IS_STRING(fileLiteral)) {
|
if (!(TOY_IS_OPAQUE(selfLiteral) || TOY_GET_OPAQUE_TAG(selfLiteral) == 900)) {
|
||||||
// interpreter->errorOutput("close(File) incorrect type for the first parameter expected: File\n");
|
interpreter->errorOutput("Incorrect argument type passed to close\n");
|
||||||
// Toy_freeLiteral(fileLiteral);
|
Toy_freeLiteral(selfLiteral);
|
||||||
// }
|
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
// FILE* fp = (FILE*)TOY_AS_OPAQUE(fileLiteral);
|
Toy_File* file = (Toy_File*)TOY_AS_OPAQUE(selfLiteral);
|
||||||
|
|
||||||
// Toy_freeLiteral(fileLiteral);
|
int result = fclose(file->fp);
|
||||||
|
|
||||||
|
// return the result
|
||||||
|
Toy_Literal resultLiteral = TOY_TO_BOOLEAN_LITERAL(result > 0);
|
||||||
|
Toy_pushLiteralArray(&interpreter->stack, resultLiteral);
|
||||||
|
|
||||||
|
// cleanup
|
||||||
|
Toy_freeLiteral(selfLiteral);
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,5 +14,5 @@ import fileio;
|
|||||||
// file.position();
|
// file.position();
|
||||||
// file.mode();
|
// file.mode();
|
||||||
|
|
||||||
// close(file);
|
// file.close();
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user