I get the following errors from Valgrind as the first ones which I can't understand:
==16606== Invalid write of size 8
==16606==    at 0x7EB9660: ??? (in /usr/lib/xorg/modules/drivers/i965_dri.so)
==16606==    by 0x7EBCAD7: ??? (in /usr/lib/xorg/modules/drivers/i965_dri.so)
==16606==    by 0x797884D: ??? (in /usr/lib/xorg/modules/drivers/i965_dri.so)
==16606==    by 0x7B70B96: ??? (in /usr/lib/xorg/modules/drivers/i965_dri.so)
==16606==    by 0x7B73968: ??? (in /usr/lib/xorg/modules/drivers/i965_dri.so)
==16606==    by 0x7B79444: ??? (in /usr/lib/xorg/modules/drivers/i965_dri.so)
==16606==    by 0x4FF4609: ??? (in /usr/lib/libSDL2-2.0.so.0.12.0)
==16606==    by 0x4FEC69E: ??? (in /usr/lib/libSDL2-2.0.so.0.12.0)
==16606==    by 0x4FF0C45: ??? (in /usr/lib/libSDL2-2.0.so.0.12.0)
==16606==    by 0x138141: textureFromText__oH9bIq9aVWxxLMPOKZa5z9bFQ (@mtextures.nim.c:344)            # this is the error
==16606==    by 0x13AF13: main__e8Zc9b6ypCaQRJuBLKbydsw (@mkorovan.nim.c:1377)
==16606==    by 0x13C93F: NimMainModule (@mkorovan.nim.c:1912)
==16606==  Address 0x7fccd34f9030 is not stack'd, malloc'd or (recently) free'd
In my application I have the following piece of code:
import sdl2, sdl2/ttf
type
  Texture* = ptr TextureObj
  TextureObj = object
    data*: TexturePtr # from sdl2 lib
    width*, height*: int
proc textureFromText*(r: RendererPtr; font: FontPtr, text: string,
                      fg: tuple[r,g,b,a: int]): Texture =
  let surf: SurfacePtr = font.renderTextSolid(
    text.cstring,
    (fg.r.uint8,fg.g.uint8,fg.b.uint8,fg.a.uint8)
  )
  let texture: TexturePtr = r.createTextureFromSurface(surf)                      # problem line
  result = cast[Texture](alloc0(sizeof(Texture)))
  result.data = texture
  result.width = surf.w
  result.height = surf.h
  freeSurface(surf)
which compiles to
N_LIB_PRIVATE N_NIMCALL(tyObject_TextureObj__s9blP5sVYLYsZltL4Bb1yzA*, textureFromText__oH9bIq9aVWxxLMPOKZa5z9bFQ)(tyObject_RendererPtrcolonObjectType___RZ5I89cPVLRdJchBQYVCSfg* r, tyObject_FontPtrcolonObjectType___aRlNW02x3t1nWe9bTYP3kng* font, NimStringV2 text, tyTuple__FtZxsWeKlOo83uVL9c4OOmg* fg) {
        tyObject_TextureObj__s9blP5sVYLYsZltL4Bb1yzA* result;
        tyObject_Surface__sOGmuBWrSv9c4mZ0oaW1R1w* surf;
        tyTuple__Xo4YhcsfLAmbGDNu1Obfcw T1_;
        tyObject_TexturePtrcolonObjectType___6n0oqQPDsaMFNhtiJ29bOXw* texture;
        void* T2_;
        nimfr_("textureFromText", "/home/grfork/playground/nim/korovan/src/textures.nim");
        result = (tyObject_TextureObj__s9blP5sVYLYsZltL4Bb1yzA*)0;
        nimln_(31, "/home/grfork/playground/nim/korovan/src/textures.nim");
        T1_.Field0 = ((NU8) ((*fg).Field0));
        T1_.Field1 = ((NU8) ((*fg).Field1));
        T1_.Field2 = ((NU8) ((*fg).Field2));
        T1_.Field3 = ((NU8) ((*fg).Field3));
        surf = Dl_16561790_(font, nimToCStringConv(text), T1_);
        nimln_(38, "/home/grfork/playground/nim/korovan/src/textures.nim");
        texture = Dl_16525674_(r, surf);                                         // problem line
        nimln_(42, "/home/grfork/playground/nim/korovan/src/textures.nim");
        T2_ = (void*)0;
        T2_ = alloc0Impl__WrVG9abV9chlTLqkiXYLKwUg_3(((NI) 8));
        result = ((tyObject_TextureObj__s9blP5sVYLYsZltL4Bb1yzA*) (T2_));
        nimln_(43, "/home/grfork/playground/nim/korovan/src/textures.nim");
        (*result).data = texture;
        nimln_(44, "/home/grfork/playground/nim/korovan/src/textures.nim");
        (*result).width = ((NI) ((*surf).w));
        nimln_(45, "/home/grfork/playground/nim/korovan/src/textures.nim");
        (*result).height = ((NI) ((*surf).h));
        nimln_(46, "/home/grfork/playground/nim/korovan/src/textures.nim");
        Dl_16525938_(surf);
        popFrame();
        return result;
}
and the signature from SDL2 library for createTextureFromSurface is
proc createTextureFromSurface*(renderer: RendererPtr; surface: SurfacePtr): TexturePtr {.
  importc: "SDL_CreateTextureFromSurface".}
In application code textureFromText is called in a loop many times a second and its resulting object is destroyed every time with the destroy proc:
proc destroy*(texture: var Texture) =
  if texture.isNil: return
  if texture.data != nil: destroy texture.data
  texture.data = nil
  dealloc texture
  texture = nil
and removing it doesn't make a difference.
I'm just learning to use Valgrind and I'm not sure I understand what is going on. How does this error occur?