Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

aboeing

odd memory issue

This topic is 5332 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I''m having a problem in that an object is being created, which I dont know how to delete, and am just wondering if anyone can tell me why its happening, and what I can do to stop it. some pseudo-code of what i''m doing:
class AbstractClass {
 virtual void Virtual_Function();
}

//"main":

AbstractClass *ac = (AbstractClass *) a_void_ptr_to_a_concrete_object;
staticSomeFunction(ac);

//"somefunction":

staticSomeFunction(AbstractClass *ac) {
 ac->Virtual_Function();
}
What happens here, is that when the ac->Virtual_Function is called it constructs a new object. This object is never destroyed. I''m not sure why this is happening, or what I can do to work around it. Has anyone encountered this problem before, and what can I do to fix it?

Share this post


Link to post
Share on other sites
Advertisement
I put a printf statement into the constructor and destructor.
Every time a virtual function is called, a new object is created (verified by printing "this"). These objects never have thier destructors called, so I''m assuming they are never deleted.

Share this post


Link to post
Share on other sites
Are you delete''ing a void pointer?
Destructors don''t get called in that case, you need to cast them back to the correct type (or base type if the dtor is virtual) before deletion.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

Share this post


Link to post
Share on other sites
No. I''m not deleting anything. I''m not creating anything either.
When ever I call a method in the "base" class a new object is being created, the method is being called, and then that object that was just created just lingers around.
ie: when I call ac->Virtual_Function() a new object is constructed.
If I cast it to the actual concrete object then the function works. (unfortunately in the "real" program I cant just do this)
eg:

staticSomeFunction(AbstractClass *ac) {
ConcreteClass *cc=(ConcreteClass *)ac;
cc->Virtual_Function(); //operates as normal

}

Share this post


Link to post
Share on other sites
I think you need to give us specific code. We can''t really see what''s going on by your examples. Give us the smallest compilable example so that we may test it ourselves.

Share this post


Link to post
Share on other sites
Ha! Once again we can knock this one up to stupidity on my part. The program had gotten so large that I had not realised that some of the objects I was dealing with had multiple inheritance.

Thanks to everyone who tried to help figure this one out.

fizban: I didn''t want to have to write an example to illustrate the problem because it would have been complicated, but now that I know what it is, its very easy to demonstrate. If it wern''t for your suggestion I probably would not have realised! Thanks!

For those who are intersted:

#include <stdio.h>

class BaseOne {
public:
virtual void BaseOneFunc() = 0;
};

class BaseTwo {
public:
virtual void BaseTwoFunc() = 0;
};

class AbstractCombined: public BaseOne, public BaseTwo {
public:
virtual void SomeFunc() = 0;
};

class ConcreteCombined: public AbstractCombined {
public:
ConcreteCombined() {
printf("hello from CC-c %x\n",this);
}
~ConcreteCombined() {
printf("goodbye from CC-d %x\n",this);
}
void SomeFunc() {
printf("%x says hi\n",this);
}
void BaseOneFunc() {
printf("%x: BaseOneFunc\n",this);
}
void BaseTwoFunc() {
printf("%x: BaseTwoFunc\n",this);
}
};

int main(int argc, char *argv[]) {
ConcreteCombined *pcc=new ConcreteCombined;
pcc->SomeFunc();

void *pv=pcc;
BaseTwo *pbt = (BaseTwo *) pv;
pbt->BaseTwoFunc(); //not the same error, but a different one now.


delete pcc;
}


So basically what happens is that you cant just cast it to a "BaseTwo" as the virtual functions will not point to the correct places. If you compile that example, chances are you get the same error I got, in that the BaseOneFunc is called, not BaseTwoFunc.

For some reason in my other program the function I was trying to use must have been remapped to a constructor or something and so it started creating objects everywhere.

Share this post


Link to post
Share on other sites
Well, what do you expect when using a void pointer? You remove all information the compiler has about a certain pointer by doing the cast using void.

Share this post


Link to post
Share on other sites
Also make sure you define a virtual destructor ''~destructor'' in your base classes. These get called whenever an object that inherits from it gets destoryed. If you don''t have a virtual destructor for the base classes then only the class that you inherited from destructor gets called when you destory it.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!