Chasing memory issues.

This commit is contained in:
Scott Duensing 2022-12-14 21:33:24 -06:00
parent 72a68fae8b
commit c3491d973f
4 changed files with 27 additions and 16 deletions

View file

@ -27,4 +27,7 @@
#include "../thirdparty/stb_ds.h"
#define ARRFREE(a) do { if (a != NULL) { arrfree(a); } break; } while(1)
#endif // ARRAY_H

View file

@ -29,7 +29,7 @@
#include "array.h"
#ifdef DEBUG_OUTPUT
#ifdef DEBUG_OUTPUTx
#define debug(...) printf(__VA_ARGS__)
#define MEMWATCH
#else

View file

@ -720,14 +720,7 @@ int vecparser(char *programIn, VecByteCodeT *bytecode) {
}
// Unwind variables array if needed.
/*
if (variables != NULL) {
while (arrlen(variables) > 0) {
arrdel(variables, 0);
}
}
*/
arrfree(variables);
ARRFREE(variables);
// Unwind unresolved array if needed.
if (unresolved != NULL) {

View file

@ -122,6 +122,7 @@ EVENT void menuVectorFileOpen(GtkWidget *object, gpointer userData);
EVENT void menuVectorFileSave(GtkWidget *object, gpointer userData);
EVENT void menuVectorFileSaveAs(GtkWidget *object, gpointer userData);
EVENT void menuVectorHelpVector(GtkWidget *object, gpointer userData);
static void releasePointList(VectorDataT *self);
static void renderBytecode(VecByteCodeT *bytecode, VectorDataT *self);
EVENT void scaleVectorTraceImageValueChanged(GtkWidget *object, gpointer userData);
static void setDirty(VectorDataT *self, gboolean dirty);
@ -386,6 +387,11 @@ EVENT void editorVectorNotify(GtkWidget *sciWidget, gint ctrlID, struct SCNotifi
//debug("Notification %d\n", notifyData->modificationType);
// Initialize bytecode data.
byteCode.bytes = NULL;
byteCode.length = 0;
byteCode.bufferSize = 0;
switch (notifyData->nmhdr.code) {
case SCN_MODIFIED:
if (notifyData->modificationType & SC_MOD_INSERTTEXT || notifyData->modificationType & SC_MOD_DELETETEXT) {
@ -399,9 +405,6 @@ EVENT void editorVectorNotify(GtkWidget *sciWidget, gint ctrlID, struct SCNotifi
// Fetch code.
SSM(SCI_GETTEXT, length, (sptr_t)self->buffer);
// Parse code.
byteCode.bytes = NULL;
byteCode.length = 0;
byteCode.bufferSize = 0;
lineNumber = vecparser(self->buffer, &byteCode);
if (lineNumber >= 0) {
// Mark lines that fail to parse.
@ -569,7 +572,7 @@ static void loadTraceImage(VectorDataT *self, char *filename) {
cairo_surface_flush(temp);
pixels = cairo_image_surface_get_data(temp);
offset = 0;
while (offset < x * y * 4) {
while (offset < x * y * 4 - 4) { // Not sure why we need the -4 but it keeps us from going one byte off the end of pixels[].
pixels[offset + 2] = image[offset ]; // R
pixels[offset + 1] = image[offset + 1]; // G
pixels[offset ] = image[offset + 2]; // B
@ -834,6 +837,17 @@ EVENT void menuVectorHelpVector(GtkWidget *object, gpointer userData) {
}
static void releasePointList(VectorDataT *self) {
PointT *temp;
while (arrlen(self->pointList) > 0) {
temp = self->pointList[0];
arrdel(self->pointList, 0);
DEL(temp);
}
}
static void renderBytecode(VecByteCodeT *bytecode, VectorDataT *self) {
int x1;
int y1;
@ -856,7 +870,7 @@ static void renderBytecode(VecByteCodeT *bytecode, VectorDataT *self) {
debug("-----------------------------------\n");
// Clear collected points array.
arrfree(self->pointList);
releasePointList(self);
// Reset JoeyLib draw state.
jlPaletteDefault(self->jlc);
@ -1143,10 +1157,10 @@ static void renderBytecode(VecByteCodeT *bytecode, VectorDataT *self) {
debug("Points: %ld\n", arrlen(self->pointList));
// Clear variables.
arrfree(self->variables);
ARRFREE(self->variables);
// Clear stack.
arrfree(stack);
ARRFREE(stack);
// Refresh widget.
gtk_widget_queue_draw(self->drawVectorImage);
@ -1550,6 +1564,7 @@ static void winVectorDelete(gpointer userData) {
cairo_surface_destroy(self->scaled);
cairo_surface_destroy(self->target);
if (self->trace != NULL) cairo_surface_destroy(self->trace);
releasePointList(self);
DEL(self->filename);
DEL(self->title);
DEL(self->tracename);