Jump to content


Photo

Bug when ToBex call c++ constructors/destructors


  • Please log in to reply
3 replies to this topic

#1 Insomniator

Insomniator
  • Modder
  • 358 posts

Posted 27 October 2020 - 12:31 PM

Hi,
 
I found an interesting bug in this amazing project, but since project is frozen, there is no one to fix/do something with it...
 
So.....how tobex call existing function from bgmain.exe ?
it's almost easy, it declare place(via template<...>) for pointer  and put adress to it:

char (EngineClass::*EngineClass_GetSong)(short) =
 SetFP(static_cast<char (EngineClass::*)(short)> (&EngineClass::GetSong), 0x4D40D6);

To call this complex template tobex uses next line, this is C usual way to call any run-time adress

char EngineClass::GetSong(short id)  { (this->*EngineClass_GetSong)(id); }

 

What's problem ?
Problem is when line {(this->*EngineClass_GetSong)(id);} used for constructors/destructors.
For example class EngineClass may contain embedded variables(fields) with other classes:

class EngineClass {
    EngineClass::EngineClass()  - constructor
    EngineClass::~EngineClass() - destructor

    CString MyString;        - MyString object embedded to EngineClass
}

Ofter tobex also call original EngineClass::EngineClass and EngineClass::~EngineClass as {this->*XXX()} , XXX is original adress inside bgmain.exe and this XXX usually do all work to create/delete EngineClass class.

 

At this point we must remember what compiler will check all vars inside EngineClass for possible other classes and will add calls to constructors of these classes before execution first programmer's code !

 

so line {this->*XXX()} will be transformed to:

- for constructor:

  1) call  CString::CString for MyString

  2) call constructorX for variable X

   .....

  N) this->*XXX()

 

- for destructor:

  1) this->*~XXX()

  2) call  CString::~CString for MyString

   ....

  N) call destructorX for variable X

 

XXX and ~XXX written and compiled by BioWare in same MS compiler (VC6 i think), so original code also will call all embedded constructors/destructors. Bug is doubling calls, tobex and game for each class variable can do same job twice !

 

Probably this is not serious problem, no crashdumps :) , but in worst case tobex can free again allready free memory because first time memory has been released by game (~XXX), second time in tobex by "hidden" code, generated by compiler.


Edited by Insomniator, 27 October 2020 - 12:32 PM.


#2 Magus

Magus
  • Member
  • 54 posts

Posted 07 November 2020 - 11:37 PM

You got the code, the repo - what else do you need? A special invitation? You got it.



#3 The Imp

The Imp

    Not good, see EVIL is better. You'll LIVE.

  • Member
  • 5148 posts

Posted 08 November 2020 - 01:39 AM

You got the code, the repo - what else do you need? A special invitation? You got it.

Erhm, you might want to clarify that you mean with this ? Is it that as the GitHub is meant to be freely spred it's coding bases, one can make their own repos as on offshot, and as anyone that has the cabability to fix used code, they are encouraged to do so in their own repos and then market those as better versions of the original, unless the original user comes back as they can intergrate the sub-repo into the main one.


Edited by The Imp, 08 November 2020 - 01:40 AM.

Yep, Jarno Mikkola. my Mega Mod FAQ. Use of the BWS, and how to use it(scroll down that post a bit). 
OK, desert dweller, welcome to the sanity, you are free to search for the limit, it's out there, we drew it in the sand. Ouh, actually it was still snow then.. but anyways.


#4 Insomniator

Insomniator
  • Modder
  • 358 posts

Posted 08 November 2020 - 05:49 PM

You got the code, the repo - what else do you need? A special invitation? You got it.

Relax, post is message in the bottle, bottle for those who (probably in future) will dig into tobex