Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Deleting objects  (Read 2293 times)

0 Members and 2 Guests are viewing this topic.

LifePhilPsyPro

  • Newbie
  • *
  • Posts: 3
    • View Profile
Deleting objects
« on: July 18, 2013, 02:48:28 pm »
Why is it that i cant delete any objects in the
Quote
while (window.isOpen())
loop?
Whenever i try to delete an object. Now matter what the object is or how it is defined the Program just crashes with an error of
Quote
_BLOCK_TYPE_IS_VALID (pHead->nBlockUse)
.
The loop might be fully empty (except the delete function) and it will crash anyways.
Thanks in advance.

Gobbles

  • Full Member
  • ***
  • Posts: 132
    • View Profile
    • Email
Re: Deleting objects
« Reply #1 on: July 18, 2013, 03:09:00 pm »
Are you trying to potentially double deleting something?

Post a a minimal and complete sample and we can figure this out.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Deleting objects
« Reply #2 on: July 18, 2013, 03:12:05 pm »
http://lmgtfy.com/?q=_BLOCK_TYPE_IS_VALID ;)

If you delete an object in a while loop without any checks, then you'll delete the object every single loop iteration and as we all know, you should never delete an object twice.

Besides that you should definitely take a look at RAII and understand how the new/delete pairs are outdated, due to it's dangers and better alternatives. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

LifePhilPsyPro

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Deleting objects
« Reply #3 on: July 18, 2013, 03:23:24 pm »
Thanks guys for the quick reply.
Well i do check,to make sure i am not deleting twice.
Quote
      int DeadAlive = Dragon->TestIfDead();
      MonPtr = Tail;
      if(DeadAlive == true)
      {
         if(Tail == Dragon){
            MonPtr = Dragon;
            Dragon = Dragon->Node;
            free(MonPtr);
            delete MonPtr;
         }
         else if(Dragon->Node == NULL){
            MonPtr = Dragon;
            Dragon = Tail;
            free(MonPtr);
            delete MonPtr;
         }
         else{
            for(short NDC = 0; NDC < DC; NDC++){
               MonPtr = MonPtr->Node;
               if(NDC + 1 == DC){
                  Dragon = MonPtr;
                  Dragon->Node = Dragon->Node->Node;
               }
            }
            free(MonPtr);
            delete MonPtr;
         }
      }

Here is a part of the code. THe one that is crashing...
Dragon is an object. Well,actualy its a linked list. Tail is obviously the pointer to the first Object of the linked list and MonPtr is just a ptr that is used to delete dead dragons.
So as you see i check if the monster is dead,if it is i try to delete it.
So as much as i can tell i am not deleting twice. Is there something i am missing? :)


eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Deleting objects
« Reply #4 on: July 18, 2013, 03:33:26 pm »
But you are aware that delete does not set the pointer to NULL, right? ???
Also what does free(...) do?

Again I strongly advise you against the use of manual memory management! ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

LifePhilPsyPro

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Deleting objects
« Reply #5 on: July 18, 2013, 03:40:29 pm »
I hear what you are saying. Thanks!
Ill try and fix that,and hopefully will learn RAII asap :)