SDL_BlitSurface and refcount

Hi,
I am trying to track down a bug in some SDL code (I’m using 2.0.10) and in the process was surprised to see the refcount of my target surface up at around 60. Is it expected behaviour for

SDL_BlitSurface(source, 0, target, 0);

to increment the refcount of target? Just because I’ve blitted source to target does that necessarily mean that target ‘has a reference’ to source?

(As I call FreeSurface on my source~s the refcount of target does decrease)

I doubt this is the cause of my problem, but was wondering about the rationale here. Am I just missing some subtle requirement?

Also, where in the source does this increment happen? a grep for ‘refcount’ doesn’t seem to find it.

Thanks,

Here’s a very simple example.

#include "SDL.h"

void test(int x)
{
    SDL_Surface *image1 = SDL_CreateRGBSurface(0, 100,100, 32, 0, 0, 0, 0);
    SDL_Surface *image2 = SDL_CreateRGBSurface(0, 100,100, 32, 0, 0, 0, 0);
    printf("%d load: image1 %d image2 %d\n", x, image1->refcount, image2->refcount);
    SDL_BlitSurface(image1, 0, image2, 0);
    printf("%d blit: image1 %d image2 %d\n", x, image1->refcount, image2->refcount);
    if (x == 0)
    {
        SDL_FreeSurface(image1);
        SDL_FreeSurface(image2);
    }
    else
    {
        SDL_FreeSurface(image2);
        SDL_FreeSurface(image1);
    }
    printf("%d free: image1 %d image2 %d\n", x, image1->refcount, image2->refcount);
}

int main(int argc, char* argv[])
{
    test(0);
    test(1);
}

Compile with

g++ -o sdleg3 sdleg3.c -LSDL2 -I/usr/include/SDL2 -lSDL2

Output is

0 load: image1 1 image2 1
0 blit: image1 1 image2 2
0 free: image1 0 image2 0
1 load: image1 1 image2 1
1 blit: image1 1 image2 2
1 free: image1 0 image2 -1

So deleting the target surface first leads to a refcount of -1