Skip to content

Commit

Permalink
Update to not leak
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanisham committed Dec 3, 2023
1 parent b5e98d7 commit 79b593f
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 11 deletions.
2 changes: 1 addition & 1 deletion include/vm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Runtime {
Runtime();
WrenInterpretResult execute(const std::string& code, const std::string& module = "main");
void repl();
void setEntryPoint(const std::filesystem::path& target);
void setEntryPoint(const std::string_view target);
static void setProgramArgs(int argc, char** argv);

private:
Expand Down
2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ int main(int argc, char* argv[]) {
vm::Runtime::setProgramArgs(argc, argv);

vm::Runtime runtime {}; // Move the vector to the runtime instead of copying
runtime.setEntryPoint(target);
runtime.setEntryPoint(target.c_str());

std::ifstream file(target);
if (!file.is_open()) {
Expand Down
16 changes: 7 additions & 9 deletions src/vm/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
#include <cstring>
#include <filesystem>
#include <iostream>
#include <linux/limits.h>
#include <memory>
#include <stdexcept>
#include <string>
#include <tuple>

int programArgCount;
std::unique_ptr<lib::os::ArgHolder> programArgsHolder;
std::unique_ptr<char[]> sourceFile;
char sourceFile[PATH_MAX];

void vm::Runtime::setProgramArgs(int argc, char** argv) {
// Initialize the ArgHolder with command line arguments
Expand Down Expand Up @@ -97,7 +98,7 @@ static void writeFn(WrenVM* vm, const char* text) {
}

static void errorFn(WrenVM* vm, WrenErrorType errorType, const char* module, const int line, const char* msg) {
const char* moduleDisplay = (std::strcmp(module, "main") == 0) ? sourceFile.get() : module;
const char* moduleDisplay = (std::strcmp(module, "main") == 0) ? sourceFile : module;
switch (errorType) {
case WREN_ERROR_COMPILE: {
printf("[%s:%d] [Error] %s\n", moduleDisplay, line, msg);
Expand Down Expand Up @@ -331,15 +332,12 @@ WrenForeignMethodFn bindForeignMethodFn(WrenVM* vm, const char* module, const ch
return nullptr;
}

void vm::Runtime::setEntryPoint(const std::filesystem::path& target) {
this->entryPoint = target;
auto relStr = target.relative_path().c_str();

// Allocate memory for sourceFile using a smart pointer
sourceFile = std::make_unique<char[]>(std::strlen(relStr) + 1);
void vm::Runtime::setEntryPoint(const std::string_view target) {
this->entryPoint = std::filesystem::path(target);
auto relStr = this->entryPoint.relative_path().c_str();

// Copy the string
std::strcpy(sourceFile.get(), relStr);
std::strcpy(sourceFile, relStr);
};

vm::Runtime::Runtime() {
Expand Down

0 comments on commit 79b593f

Please sign in to comment.