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

Author Topic: .dispose()  (Read 6368 times)

0 Members and 1 Guest are viewing this topic.

RobotGymnast

  • Jr. Member
  • **
  • Posts: 56
    • View Profile
.dispose()
« on: February 03, 2011, 01:01:47 pm »
Does one need to bother disposing everything using .dispose(), or is it just if I want to invalidate it prematurely?

JAssange

  • Full Member
  • ***
  • Posts: 104
    • View Profile
Re: .dispose()
« Reply #1 on: February 03, 2011, 09:09:25 pm »
Quote from: "RobotGymnast"
Does one need to bother disposing everything, or is it just if I want to invalidate it prematurely?

If D is anything like C anything that isn't an explicitly allocated pointer goes out of scope with the pair of brackets it's declared in.

RobotGymnast

  • Jr. Member
  • **
  • Posts: 56
    • View Profile
.dispose()
« Reply #2 on: February 07, 2011, 11:59:54 pm »
So it turns out that it's completely necessary to dispose everything, like Images, otherwise memory leaks result, and exceptions are soon thrown, as demonstrated in this simple piece of code:

Code: [Select]

import dsfml.graphics.all;

void main()
{
while(true)
auto rect = new Image(1024, 1024);
}



We can use .dispose() inside the loop, to free the allocated memory;
the only problem is, this throws exceptions and/or gives access violations when the objects are ACTUALLY destroyed, as demonstrated in:

Code: [Select]

import dsfml.graphics.all;

void main()
{
      auto rect = new Image(1024, 1024);
      rect.dispose();
}


What's the proper procedure here? Either memory leaks or access violations?

Edit: Fixed by calling GC.collect() manually once-in-a-while.

deadalnix

  • Newbie
  • *
  • Posts: 45
    • View Profile
.dispose()
« Reply #3 on: June 15, 2011, 07:04:53 pm »
GC.collect is called automatically.

It's triggered when you try to allocate memory and the GC has no mor eleft. In this case, the GC try to free memory before asking more to the system.