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

Author Topic: Problem disposing objects that inherit from the Transformable class.  (Read 2308 times)

0 Members and 1 Guest are viewing this topic.

Wekaj

  • Newbie
  • *
  • Posts: 2
    • View Profile
Hi, I had been having a problem with SFML recently in that my application would throw an exception when I closed it. This exception seemed to be related to the destruction of the sole SoundBuffer that I had loaded in my application, and the exception did not appear when I didn't load or use the SoundBuffer. Disposing any Sound objects that used this SoundBuffer before the application closed seemed to prevent the exception from being thrown, and therefore fixed this problem.

A new problem arises, however, from the fact that the objects (SceneNodes) that hold these Sound objects inherit from the Transformable class:

internal class SceneNode : Transformable, Drawable
{
    ...
}

This means that to implement the dispose pattern in these SceneNodes, I have had to override the Destroy(bool disposing) method that the Transformable class provides:

protected override void Destroy(bool disposing)
{
    if (disposed)
        return;

    if (disposing)
        sound.Dispose();

    disposed = true;
    base.Destroy(disposing);
}

The problem is that this Destroy method is never called, even after calling Dispose on the SceneNodes, and my SceneNodes can therefore not dispose of their Sound objects.

The Transformable class is a child of the ObjectBase class, with the ObjectBase class being the first in the hierarchy to implement the dispose pattern. I found that the Destroy method is only called when the ObjectBase's pointer is not zero:

private void Dispose(bool disposing)
{
    if (myThis != IntPtr.Zero)
    {
        Destroy(disposing);
        myThis = IntPtr.Zero;
    }
}

Using a debugger, I found that my SceneNodes all had zero pointers, which is why their Destroy methods were never called. Passing an IntPtr with a value of 1 in the constructors of my SceneNodes seemed to fix this problem and allow them to successfully dispose of their Sound objects, but knowing nothing about the pointer system myself, this seems to be a temporary fix that could have possible side effects in the future.

public SceneNode()
    : base (new IntPtr(1))
{
}

Is the fact that all of my SceneNodes have zero pointers by default, preventing them from utilizing the Destroy method, normal? Do I have to manually declare their pointers when I construct them to fix this problem? Should I just define my own disposing method in my SceneNodes and forget about following the dispose pattern in this case?

Thank you.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Problem disposing objects that inherit from the Transformable class.
« Reply #1 on: October 02, 2016, 09:26:18 am »
You should forget about how SFML handles disposing, which is specific to its classes, and directly override Dispose with your own implementation.
Laurent Gomila - SFML developer

Wekaj

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Problem disposing objects that inherit from the Transformable class.
« Reply #2 on: October 02, 2016, 01:37:31 pm »
Ok, cool, thank you for the reply. Sorry if it was a dumb question!