Advertisement

Pointer and class problems

Started by June 07, 2000 01:25 PM
2 comments, last by Sludge 24 years, 6 months ago
I have created a class like this:

class TEST{
   private:
      TEST *sptr;
   public:
      TEST(TEST *);
      void Release(void);
};

void CreateClass(TEST *&test){
   test=new TEST(test);
};
It is like creating objects in DirectX but there is one problem:

TEST::TEST(TEST *tptr){
   sptr=tptr;
};

void TEST::Release(void){
   delete(sptr);
};
The problem is that it just deletes the class before it can exit from the function giving a memory access violation. This the way I would solve it in assembler: -Saving the instruction pointer before you enter the Release member function. -In release save the old instruction pointer, the object pointer (sptr) and the object type on the stack and jump to another function. -Then delete the object and jump to the place where you called the Release member function. Anyone has an idea how to do this in C++? Sludge Software www.sludgesoft.com Developing a secret of mana style role-playing-game
Sludge Softwarewww.sludgesoft.comDeveloping a secret of mana style role-playing-game
You appear to be storing a pointer to the class within that class. Is there any reason why you are doing this and not just using the classes own internal "this" pointer?

If you could get away with using the "this" pointer then your release method would simply look something like

void TEST::Release(void)
{
delete this;
}

which seems to work on most compilers (I''m not sure its part of the C++ specification but you can do it safely).


Regards,
Ralph Potter
ralph.potter@digital-magi.com, http://www.digital-magi.com

Regards,Ralph Potterralph.potter@digital-magi.com, http://www.digital-magi.com
Advertisement
To me, one of the problems seems to be that you''re using the old value of "test" (before it gets initialized) for "tptr", which means that in the end you''re deleting an uninitialized pointer (possibly NULL). Also, if you really want to do things the DirectX way (but why? ...), there probably are documents for creating COM stuff.
I didn''t know about the internal this pointer. It isn''t mentioned anywhere in my C++ book, so thanks for this piece of extra knowledge. The problem is also solved now.
The reason I want to do it the DirectX way is that I''m programming this library (except myself) for a friend who programs in DJGPP (it is made for DJGPP). He wants to learn DirectX after DJGPP so this is a good practise.

Sludge Software
www.sludgesoft.com
Developing a secret of mana style role-playing-game
Sludge Softwarewww.sludgesoft.comDeveloping a secret of mana style role-playing-game

This topic is closed to new replies.

Advertisement