C Memory

Johannes Pfau nospam at example.com
Sun May 5 01:18:15 PDT 2013


Am Sun, 05 May 2013 09:28:05 +0200
schrieb "Diggory" <diggsey at googlemail.com>:

> On Sunday, 5 May 2013 at 07:23:25 UTC, Namespace wrote:
> > On Sunday, 5 May 2013 at 06:43:17 UTC, Diggory wrote:
> >> On Sunday, 5 May 2013 at 06:35:38 UTC, Namespace wrote:
> >>> Quick question: I have a SDL_Surface in one of my classes and 
> >>> the SDL_Surface contains (obviously) memory to the pixel 
> >>> data. Since I cannot free this memory with the DTor: what 
> >>> will happen? AFAIK this cannot be freed by the GC because it 
> >>> was not allocated by it. So AFAIK this creates a memory leak. 
> >>> Am I right?
> >>
> >> Why can't you free the memory from the destructor?
> >
> > Every time I tried this, I got this: 
> > core.exception.InvalidMemoryOperationError
> > I do not know if I get that, because the GC has already cleaned 
> > this store or because of something else.
> 
> Sounds like a bug - if the pointer is not into GC memory then the 
> GC shouldn't touch it.

You have to provide some more details here:

* How was the SDL_Surface allocated?
* What function do you use to free it?
* When does the InvalidMemoryOperationError occur? Exactly when you
  call the free funtion or later (likely a garbage collection run)?

I guess you got the SDL_Surface from SetVideoMode or some similar
SDL function. Then it was allocated by SDL and likely not with the GC
(unless SDL allows to provide custom allocators and you did that). You
probably tried to free it with GC.free or 'delete' which only works for
GC allocated memory?

Anyway, if the Surface was allocated by SDL you should use
http://sdl.beuc.net/sdl.wiki/SDL_FreeSurface to free it. You can also
call this function in a destructor, you just shouldn't free GC memory
in a destructor.


More information about the Digitalmars-d-learn mailing list