delete - deleting 'void*' is undefined

Sep 2, 2011 at 5:40pm
I got a warning in my compiler build log
src/gCalcFunc.cpp:567: warning: deleting 'void*' is undefined

I tried to apply the patch http://pastebin.com/pcTtwXTp in order to have multiple open ofstreams which I can all close on a SIGTERM.

But due to the warning above, I'm not sure if I really do give the memory free at the end.

Should I write something like
delete (*((std::ofstream*)*ofs_iterator));
instead of
delete (**ofs_iterator);
?
Sep 2, 2011 at 5:51pm
deleting a void* is a bad idea. delete needs to call the destructor of whatever object it's destroying, and it can't do that if it doesn't know the type.

You probably shouldn't need to do any casting. Just delete whatever it is you're trying to delete normally.
Sep 2, 2011 at 8:03pm
Disch wrote:
Just delete whatever it is you're trying to delete normally.

If I try that, I get the compiler message from above which doesn't sound very good.

As you can read in the patch file I posted above, my code looks similiar to
1
2
3
4
5
6
7
8
9
10
11
12
int master(std::string filename, and much more) {
  std::vector<std::ofstream*> ofstreams;
  ofstreams.push_back(new std::ofstream(filename.c_str(), std::ios::out | std::ios::app));
  const std::vector<std::ofstream*>::iterator ofs_iterator = ofstreams.end();

  ....

  (**ofs_iterator).close();
  delete (**ofs_iterator);
  ofstreams.erase(ofs_iterator);
  return 0;
}


Further suggestions?
Sep 2, 2011 at 8:08pm
wait a minute. You're trying to delete an object.

ofs_iterator is a vector<ofstream*>::iterator
therefore
*ofs_iterator is a ofstream*
and
**ofs_iterator is a ofstream

You don't give objects to delete. You give it pointers.


try delete *ofs_iterator; // <- only 1 *
Sep 2, 2011 at 8:19pm
Oh, that's a stupid mistake. Thanks a lot! :)
Topic archived. No new replies allowed.