Original Post
I am currently trying to develop my own scripting language(Bytecode virtual machine). However, I just added in simple data parameters to my Instruction class(This hold 1 opcode and it''s data).
The class looks like this:
-Earth is 98% full. Please delete anybody you can.
class CInstruction
{
public:
CInstruction() : m_OpCode(OP_Nop), m_Data(NULL) {}
CInstruction(OpCode OpCode) : m_OpCode(OpCode), m_Data(NULL) {}
CInstruction(OpCode OpCode, const char *Data, long lDataSize)
{
m_OpCode = OpCode;
m_Data = new char[lDataSize];
memcpy(m_Data, Data, lDataSize);
}
~CInstruction()
{
if (m_Data)
{
delete [] m_Data;
m_Data = NULL;
}
}
OpCode Code() const { return (m_OpCode); }
char *Data() const { return (m_Data); }
private:
OpCode m_OpCode;
char *m_Data;
};
In my main, I am making the script by adding in CInstruction into a vector. This is my main, including execution of the script:
char *buffer = "Buffer with test data 1";
char *buff1 = "Buffer 2, more test data";
CVirtualMachine VM;
vector Instructions;
Instructions.push_back(CInstruction(OP_Print, buffer, (long)strlen(buffer) + 1));
Instructions.push_back(CInstruction(OP_Print, buffer, (long)strlen(buffer) + 1));
Instructions.push_back(CInstruction(OP_Print, buff1, (long)strlen(buff1) + 1));
Instructions.push_back(CInstruction(OP_Print, buff1, (long)strlen(buff1) + 1));
Instructions.push_back(CInstruction(OP_End));
CScript Script(Instructions);
// Load script, and execute it
VM.Execute(VM.Load(Script));
This crashes immediatly on the first push_back() call, because the destructor gets called twice and the second time the m_Data pointer is garbage. Any clues?
Toolmaker
-Earth is 98% full. Please delete anybody you can.