Skip to content

Commit 8130359

Browse files
committed
VirtualMachine: Add register leak warning
1 parent d74ba33 commit 8130359

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

src/engine/virtualmachine.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
#include "virtualmachine.h"
44

5+
#define MAX_REG_COUNT 1024
6+
57
#define DISPATCH() goto *dispatch_table[*++pos]
68
#define FREE_REGS(count) m_regCount -= count
79
#define ADD_RET_VALUE(value) *m_regs[m_regCount++] = value
@@ -20,15 +22,15 @@ using namespace vm;
2022
/*! Constructs VirtualMachine. */
2123
VirtualMachine::VirtualMachine()
2224
{
23-
m_regs = new Value *[1024];
24-
for (int i = 0; i < 1024; i++)
25+
m_regs = new Value *[MAX_REG_COUNT];
26+
for (int i = 0; i < MAX_REG_COUNT; i++)
2527
m_regs[i] = new Value();
2628
}
2729

2830
/*! Destroys the VirtualMachine object. */
2931
VirtualMachine::~VirtualMachine()
3032
{
31-
for (int i = 0; i < 1024; i++)
33+
for (int i = 0; i < MAX_REG_COUNT; i++)
3234
delete m_regs[i];
3335
delete m_regs;
3436
}
@@ -122,6 +124,9 @@ unsigned int *VirtualMachine::run(unsigned int *pos, RunningScript *script)
122124
DISPATCH();
123125

124126
do_halt:
127+
if (m_regCount > 0) {
128+
std::cout << "warning: VM: " << m_regCount << " registers were leaked by the script; this is most likely a bug in the VM or in the compiler" << std::endl;
129+
}
125130
return pos;
126131

127132
do_const:

0 commit comments

Comments
 (0)