No leaks for splash screen and text files

This commit is contained in:
2026-06-05 15:33:19 +02:00
parent 2ad55b9132
commit 63bf73f1f2
10 changed files with 33 additions and 56 deletions
-1
View File
@@ -6,7 +6,6 @@
#include <time.h> #include <time.h>
#include "define.h" #include "define.h"
#include "lisp.h" #include "lisp.h"
typedef struct lsp_client_t LspClient; typedef struct lsp_client_t LspClient;
+1
View File
@@ -27,6 +27,7 @@
#define GUTTER_WIDTH 2 #define GUTTER_WIDTH 2
enum editorKey_e { enum editorKey_e {
BACKSPACE = 127, BACKSPACE = 127,
ARROW_LEFT = 1000, ARROW_LEFT = 1000,
-1
View File
@@ -55,7 +55,6 @@ int main(int argc, char *argv[]) {
if (argc >= 2) { if (argc >= 2) {
active->buffer_id = bufferCreate(argv[1], READ_AND_WRITE); active->buffer_id = bufferCreate(argv[1], READ_AND_WRITE);
buf = &E.buffers[active->buffer_id]; buf = &E.buffers[active->buffer_id];
appDebug("project root : %s\n", dirname(buf->fullname));
} }
free(splash_screen); // Now guaranteed safe free(splash_screen); // Now guaranteed safe
+4 -12
View File
@@ -8,7 +8,6 @@
#include "../include/editor_op.h" #include "../include/editor_op.h"
#include "../include/data.h" #include "../include/data.h"
#include "include/split_screen.h" #include "include/split_screen.h"
#include <_string.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <libgen.h> #include <libgen.h>
@@ -63,8 +62,9 @@ struct buffer_t* bufferFindById(int buffer_id)
int bufferCreate(const char* path, enum bufferStatus_e state) int bufferCreate(const char* path, enum bufferStatus_e state)
{ {
appDebug("Creating new buffer"); appDebug("Creating new buffer");
char* filename = basename((char*)path); char *path_cpy = strdup(path);
char* fullname; char* filename = basename((char*)path_cpy);
char fullname[PATH_MAX + 1];
// Check if file is already open // Check if file is already open
const int existing_id = bufferFindByFilename(path); const int existing_id = bufferFindByFilename(path);
if (existing_id != -1) if (existing_id != -1)
@@ -86,13 +86,6 @@ int bufferCreate(const char* path, enum bufferStatus_e state)
{ {
return -1; return -1;
} }
new_buf->fullname = malloc(1024 * sizeof(char));
if (!new_buf->fullname)
{
free(new_buf->filename);
return -1;
}
fullname = malloc(PATH_MAX * sizeof(char));
realpath(path, fullname); realpath(path, fullname);
new_buf->fullname = strdup(fullname); new_buf->fullname = strdup(fullname);
new_buf->path = dirname(fullname); new_buf->path = dirname(fullname);
@@ -103,7 +96,6 @@ int bufferCreate(const char* path, enum bufferStatus_e state)
new_buf->dirty = 0; // New file starts clean new_buf->dirty = 0; // New file starts clean
new_buf->b_lsp_open = 0; new_buf->b_lsp_open = 0;
free(fullname);
// Load file content using existing editorOpen // Load file content using existing editorOpen
editorOpen(new_buf); editorOpen(new_buf);
@@ -111,7 +103,7 @@ int bufferCreate(const char* path, enum bufferStatus_e state)
if (new_buf->filename[strlen(new_buf->filename) - 1] == 'c') if (new_buf->filename[strlen(new_buf->filename) - 1] == 'c')
{ {
if (E.lsp_client->state == LSP_SHUTDOWN) if (E.lsp_client->state == LSP_SHUTDOWN)
lspStart(E.lsp_client, dirname(new_buf->path)); lspStart(E.lsp_client, new_buf->path);
while (E.lsp_client->state != LSP_READY); while (E.lsp_client->state != LSP_READY);
lspDidOpen(E.lsp_client, new_buf); lspDidOpen(E.lsp_client, new_buf);
} }
+13 -11
View File
@@ -15,7 +15,6 @@
#include "../include/input.h" #include "../include/input.h"
#include "../include/terminal.h" #include "../include/terminal.h"
#include "../include/split_screen.h" #include "../include/split_screen.h"
#include "../include/lisp.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@@ -24,6 +23,7 @@
#include "include/completion.h" #include "include/completion.h"
#include "include/init.h" #include "include/init.h"
/** /**
* @brief Finds a prefix configuration by name * @brief Finds a prefix configuration by name
* @details Searches the prefix array for a prefix matching the given name. * @details Searches the prefix array for a prefix matching the given name.
@@ -148,17 +148,17 @@ void bFree_structs(void)
// Similar fix for buffers // Similar fix for buffers
for (i = 0; i < E.number_of_buffer; ++i) { for (i = 0; i < E.number_of_buffer; ++i) {
free(E.buffers[i].filename); struct buffer_t *b = &E.buffers[i];
E.buffers[i].filename = NULL; for (j = 0; j < b->numrows; j++)
for (j = 0; j < E.buffers[i].numrows; ++j) { free(b->row[j].chars); // ← free each row's content
free(E.buffers[i].row[j].chars); free(b->row); // ← then free the array
E.buffers[i].row[j].chars = NULL; if (b->filename) free(b->filename);
} // if (b->path) free(b->path);
free(E.buffers[i].row); if (b->fullname) free(b->fullname);
E.buffers[i].row = NULL;
} }
// bFree layout // bFree layout
free(E.layout.panes); free(E.layout.panes);
free(E.status_msg);
free(E.init_file_path); free(E.init_file_path);
fclose(E.fd_init_file); fclose(E.fd_init_file);
@@ -188,11 +188,12 @@ Lisp editorQuit(Lisp args, LispError* e, LispContext ctx)
} }
disableRawMode(); disableRawMode();
bFree_structs(); bFree_structs();
deInitEditor();
write(STDOUT_FILENO, "\x1b[2J", 4); write(STDOUT_FILENO, "\x1b[2J", 4);
write(STDOUT_FILENO, CURSOR_TOP_LEFT, 3); write(STDOUT_FILENO, CURSOR_TOP_LEFT, 3);
lspShutdown(E.lsp_client); lspShutdown(E.lsp_client);
free(E.lsp_client);
lisp_shutdown(E.ctx); lisp_shutdown(E.ctx);
// deInitEditor();
exit(0); exit(0);
} }
@@ -221,6 +222,7 @@ Lisp l_editorSplitScreenVertical(Lisp args, LispError* e, LispContext ctx)
Lisp l_editorSave(Lisp args, LispError* e, LispContext ctx) Lisp l_editorSave(Lisp args, LispError* e, LispContext ctx)
{ {
appDebug("[EDITOR SAVE]");
editorSave(); editorSave();
return lisp_null(); return lisp_null();
} }
@@ -374,10 +376,10 @@ Lisp editorMoveCursorPageDown(Lisp args, LispError* e, LispContext ctx)
*/ */
Lisp editorOpenFile(Lisp args, LispError* e, LispContext ctx) Lisp editorOpenFile(Lisp args, LispError* e, LispContext ctx)
{ {
appDebug("[EDITOR OPEN FILE]\n");
char* filename = editorPrompt("Open : %s", getenv("PWD"), 1); char* filename = editorPrompt("Open : %s", getenv("PWD"), 1);
if (filename) if (filename)
{ {
// editorOpen(filename);
EditorPane* active = splitScreenGetActivePane(); EditorPane* active = splitScreenGetActivePane();
active->buffer_id = bufferCreate(filename, READ_AND_WRITE); active->buffer_id = bufferCreate(filename, READ_AND_WRITE);
} }
+1 -2
View File
@@ -9,7 +9,6 @@
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include <string.h> #include <string.h>
#include <sys/syslimits.h>
#include <sys/wait.h> #include <sys/wait.h>
#include <errno.h> #include <errno.h>
@@ -148,7 +147,7 @@ int lspStart(LspClient *client, const char *root_dir)
client->write_fd = stdout_pipe[0]; client->write_fd = stdout_pipe[0];
client->pid = pid; client->pid = pid;
client->next_id = 1; client->next_id = 1;
client->state = LSP_SHUTTING_DOWN; client->state = LSP_INITIALIZING;
// Send initialize request // Send initialize request
char root_uri[PATH_MAX + 8]; char root_uri[PATH_MAX + 8];
+10 -8
View File
@@ -60,7 +60,7 @@ void editorCloseFile(void) {
*/ */
void editorOpen(struct buffer_t* buffer) { void editorOpen(struct buffer_t* buffer) {
FILE *fp; FILE *fp;
fp = fopen(buffer->fullname, "a+"); fp = fopen(buffer->fullname, "r");
if (!fp) if (!fp)
die("fopen"); die("fopen");
@@ -80,6 +80,8 @@ void editorOpen(struct buffer_t* buffer) {
line = NULL; line = NULL;
line_cap = 0; // Reset for next iteration line_cap = 0; // Reset for next iteration
} }
free(line);
line = NULL;
fclose(fp); fclose(fp);
E.dirty = 0; E.dirty = 0;
} }
@@ -98,7 +100,7 @@ void editorSave() {
EditorPane *active = splitScreenGetActivePane(); EditorPane *active = splitScreenGetActivePane();
struct buffer_t *buffer = bufferFindById(active->buffer_id); struct buffer_t *buffer = bufferFindById(active->buffer_id);
int len; int len;
int fd; FILE *fd;
if (buffer->filename == NULL) { if (buffer->filename == NULL) {
buffer->filename = editorPrompt("Save as: %s (ESC to cancel)", "", 1); buffer->filename = editorPrompt("Save as: %s (ESC to cancel)", "", 1);
if (buffer->filename == NULL) { if (buffer->filename == NULL) {
@@ -106,21 +108,21 @@ void editorSave() {
return; return;
} }
} }
fd = open(buffer->fullname, O_WRONLY | O_CREAT | O_TRUNC, 0644); fd = fopen(buffer->fullname, "w");
if (fd != -1) { if (fd != NULL) {
for (int i = 0; i < buffer->numrows; ++i) for (int i = 0; i < buffer->numrows; ++i)
{ {
len = (int) strlen(buffer->row[i].chars); len = (int) strlen(buffer->row[i].chars);
if (write(fd, buffer->row[i].chars, len) != len) { if (fwrite(buffer->row[i].chars, 1, len, fd) != len) {
close(fd); fclose(fd);
editorSetStatusMessage("Can't save! I/O error: %s", strerror(errno)); editorSetStatusMessage("Can't save! I/O error: %s", strerror(errno));
return; return;
} }
write(fd, "\n", 1); fwrite("\n", 1, 1, fd);
} }
buffer->dirty = 0; buffer->dirty = 0;
close(fd); fclose(fd);
} }
editorSetStatusMessage("File saved"); editorSetStatusMessage("File saved");
} }
+2 -19
View File
@@ -149,7 +149,7 @@ void initEditor() {
strncpy(E.prefix[0].prefix_name, "no-prefix", 64); strncpy(E.prefix[0].prefix_name, "no-prefix", 64);
E.prefix_state = 0; E.prefix_state = 0;
E.lsp_client = (LspClient*)malloc(sizeof(LspClient)); E.lsp_client = (LspClient*)calloc(1,sizeof(LspClient));
E.lsp_client->state = LSP_SHUTDOWN; E.lsp_client->state = LSP_SHUTDOWN;
initConfig(); initConfig();
@@ -171,22 +171,5 @@ void initEditor() {
void deInitEditor() void deInitEditor()
{ {
freeScreenLayout(&E.layout); fclose(E.ctx.p->err_port);
free(E.lsp_client);
free(E.status_msg);
free(E.init_file_path);
free(E.key_binds);
for (int i = 0; i < E.number_of_keybinds; i++)
{
free(E.key_binds[i].key_sequence);
}
for (int i = 0; i < E.number_of_buffer; ++i)
{
free(E.buffers[i].filename);
free(E.buffers[i].path);
free(E.buffers[i].fullname);
free(E.buffers[i].row->chars);
free(E.buffers[i].row);
}
} }
+1 -1
View File
@@ -210,7 +210,7 @@ int executeKeyBind(char *key_sequence) {
*/ */
void editorProcessKeypress() { void editorProcessKeypress() {
int c = editorReadKey(); int c = editorReadKey();
char key_sequence[8]; char key_sequence[8] = {0};
EditorPane *active = splitScreenGetActivePane(); EditorPane *active = splitScreenGetActivePane();
struct buffer_t *buf = bufferFindById(active->buffer_id); struct buffer_t *buf = bufferFindById(active->buffer_id);
+1 -1
View File
@@ -179,7 +179,7 @@ int editorReadKey()
if (c == '\x1b') if (c == '\x1b')
{ {
char seq[6]; char seq[6] = {0};
/* try to read escape sequence */ /* try to read escape sequence */
if (read(STDIN_FILENO, &seq[0], 1) != 1) if (read(STDIN_FILENO, &seq[0], 1) != 1)
return '\x1b'; return '\x1b';