odd memory issue

Started by
7 comments, last by aboeing 20 years, 3 months ago
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?
Advertisement
how do yo know it is never destroyed? (or created?)
------------------------------------------VOTE Patrick O'GradyWrite in Presidential CandidateThe Candidate who Cares.
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.
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
"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley
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}
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.
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.
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.
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.
In the making Tread Wars - Tank Battle gamePowered by SKEnginehttp://www.highendgaming.com

This topic is closed to new replies.

Advertisement