Skip to main content
GameDev.net gamedev.net
🔒 Locked

vectors and temp objects(Nameless) problems

Started by Toolmaker Feb 5, 2004 at 5:17 AM 3 replies 250+ views
Original Post
Toolmaker
Toolmaker
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:
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.
 
Kylotan
Kylotan
If you''re gonna use a class in standard containers, make sure it has a valid copy constructor defined. Really you should always define a copy constructor for any class or make it uncopyable. Your copy constructor here will look much like your 3-parameter constructor.

[ MSVC Fixes | STL Docs | SDL | Game AI | Sockets | C++ Faq Lite | Boost
Asking Questions | Organising code files | My stuff | Tiny XML | STLPort]
Toolmaker
Toolmaker
Thx Kylotan! I will add a copy constructor to the class. And as a reference, I am using these articles are base of my scripting system:

http://www.gamedev.net/reference/articles/article1633.asp
http://www.gamedev.net/reference/articles/article1686.asp
http://www.gamedev.net/reference/articles/article1788.asp
http://www.gamedev.net/reference/articles/article1803.asp
http://www.gamedev.net/reference/articles/article1877.asp

I won''t make them clickities, since I am too lazy for that!

Toolmaker



-Earth is 98% full. Please delete anybody you can.
 
Enigma
Enigma
Any class with dynamically allocated data should (must) define a copy constructor and an assignment operator. Assuming you have a proper destuctor and copy constructor a quick and easy assignment operator is:
type& operator=(const type& instance)
{
// check for self assignement
if (this != &instance)
{
// call destructor explicitly
this->~type();
// call placement new to build new instance in place
new (this) type(instance);
}
return *this;
}


note that placement new requires you #include <memory>

Enigma
neoztar
neoztar
...or a safer way (assumes a non-throwing swap() member function):


type& type::operator=( const type& instance ) {
// check for self assignment
if( this != &instance )
swap( type( instance ) );
return *this;
}


Now you don''t get a destroyed object if the copy constructor throws an exception.

~nz

// Website // Google // GameDev // NeHe // MSDN // OpenGL Extensions //

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.