Jump to content
  • Advertisement

Archived

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

EDI

void * deletion / destruction problem

This topic is 5776 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

it seems that whenever i cast some object pointer to a void pointer then delete it the object''s destructor is not called. is this ''normal'' is there anything i can do about it? im trying to build an alloc and forget array class wherein the class allocates an array of void*''s that other objects get inserted into, and when the class is destroyed it will delete each pointer in the array since i dont know what kind of objects will be in the array i cant use anything other than void* i know i ''could'' use templates to have a given array adapt to a type but i dont like using templates and i would like the array to be able to contain many types of objects any help is greatly apreciated=) Raymond Jacobs, Profesional Web Applications Developer, Weekend Game Developer, www.EtherealDarkness.com

Share this post


Link to post
Share on other sites
Advertisement
to have the correct destructor called you have to use a correct pointer

using templates will solve your problem.

I take it that you mean you want to store pointers to unrelated things in your array. How will you know what they all are?

Share this post


Link to post
Share on other sites

yeah i figured templates would work in this case, darn
i hate templates lol

btw, good question i wouldnt know what they ''were''
guess i dont need that feature now=)

ok so templates it is=)

thanks

Raymond Jacobs,
Profesional Web Applications Developer,
Weekend Game Developer,
www.EtherealDarkness.com

Share this post


Link to post
Share on other sites
quote:
Original post by EDI
it seems that whenever i cast some object pointer to a void pointer then delete it the object''s destructor is not called.

That''s because you''re not supposed to do that.
quote:

is this ''normal'' is there anything i can do about it?

Yes. Don''t call delete with a void*.
quote:

wherein the class allocates an array of void*''s

Oh heck...
quote:

i know i ''could'' use templates to have a given array adapt to a type but i dont like using templates and i would like the array to be able to contain many types of objects

Then you need some means of type-discrimination, where each type that goes into the array is entered into a structure along with a tag representing the type code. Then, every time you perform an operation on the type (like deleting), you need to first cast it to the type indicated by the tag. Nice to see yet another person emulating dynamic typing in C++.

Share this post


Link to post
Share on other sites
actually, thinking about it, what you''re saying is all wrong!

when you say ''other objects get inserted into'', what exactly do you mean by that? that the object is created by the array or the object already exists?

Share this post


Link to post
Share on other sites
ok just to clear that up here is the thing,

i make a new array class

FC_Array myarray;


i insert a new object into it

myarray.InsertObject(new Sprite());


now if i want to have the array do the garbage collection for me,

then i set

myarray.WillFree(true);


internaly the array dynamicly allocs an array of void *''s

void **m_list;

problem with that is because when the array tries to free the elements it they are currently being stored as void*

which when delted doesnt call the destructor... which is my problem

now im probably going to have to implement templates which i hate to do cause i feel they complicate things and are just plain ugly=)

also

"
Then you need some means of type-discrimination, where each type that goes into the array is entered into a structure along with a tag representing the type code. Then, every time you perform an operation on the type (like deleting), you need to first cast it to the type indicated by the tag. Nice to see yet another person emulating dynamic typing in C++.
"

even using a structre wont work since the single structure cant anticipate all the possible object types ill be using






Raymond Jacobs,
Profesional Web Applications Developer,
Weekend Game Developer,
www.EtherealDarkness.com

Share this post


Link to post
Share on other sites
Really?


typedef struct {
void *data;
char dataType;
} Object;


enum dataTypes {
DATA_CHAR_ARRAY = 0
...
...
};


// during deallocation,
switch (objectInstance->dataType) {
case DATA_CHAR_ARRAY:
delete (char *)objectInstance->data;
break;
...
}


Welcome to dynamic typing!

[edited by - johnnie2 on January 23, 2003 10:17:36 AM]

Share this post


Link to post
Share on other sites
Thats all well and good but what if i make an object called

UnkownToAllButMe

the array isnt going to know how to cast to that type of object because for all accounts said object doesnt exist.


since the deletion routines must be done internaly in the class

within the destructor of the array class

i would need somthing like

case OBJECT_TYPE_UNKNOWNTOALLBUTME:
delete (UnknownToAllButMe*)lpdata;
break;


but since at the time when this array class was made the UnkownToAllButMe object hadnt even been thought of, thus i never coded a case for it





Raymond Jacobs,
Profesional Web Applications Developer,
Weekend Game Developer,
www.EtherealDarkness.com

[edited by - EDI on January 23, 2003 10:31:43 AM]

Share this post


Link to post
Share on other sites
you could make the InsertObject member function templated
It creates a wrapper round the class pointer and puts the wrapper into the array.
The wrapper is derived from some base class and also takes the type information from the template. It will know how to delete the pointer it is wrapping correctly.

or you could try boost''s any class

Share this post


Link to post
Share on other sites
quote:
Original post by EDI
even using a structre wont work since the single structure cant anticipate all the possible object types ill be using

Partially correct. You could emulate more fully a dynamic type system. Perhaps, in your pursuit for dynamic typing, you would drop the pretence of static typing and build a fuller dynamic system. Forget the type-safe new and delete operators, and use malloc and free instead. free takes a void*. However, in doing this you also need to drop the pretence of using C++ classes for creating types, and instead you create a generic structure that holds both the type identifier, and a sub-structure representing the physical make-up of the object.

No longer do you control lifetime of instances by saying:

int i = new int(10);
// ...
delete i;


But instead do something like:

arglist args;
args.push_back("10");
var i = ("i", make_instance("int", args));
// ...
delete_instance(symbol_from_string("i"));

Or maybe you could even implement a simple garbage-collection scheme to save explicitly deleting instances.

Welcome to Greenspun's tenth rule!

[edited by - SabreMan on January 23, 2003 10:38:53 AM]

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!