Search code examples
clinuxrtmp

librtmp free(): invalid pointer


I'm trying to handle packets with librtmp but get a "free(): invalid pointer" error.

#include <stdio.h>
#include <stdlib.h>
#include <librtmp/rtmp.h>
#include <librtmp/log.h>

int main(){
    RTMP *r;
    RTMPPacket packet;

    char uri[] = "rtmp://167.114.171.21:1936/tinyconf app=tinyconf timeout=180000 live=1 conn=S:ROOMNAME swfurl=http://tinychat.com/embed/Tinychat-11.1-1.0.0.0602.swf";

    RTMP_LogLevel loglvl=RTMP_LOGDEBUG2;
    RTMP_LogSetLevel(loglvl);

    r = RTMP_Alloc();
    RTMP_Init(r);
    RTMP_SetupURL(r, (char*)uri);
    RTMP_Connect(r, NULL);

    while (RTMP_IsConnected(r)) {
        RTMP_ReadPacket(r, &packet);
        if (!RTMPPacket_IsReady(&packet))
            continue;
        RTMP_ClientPacket(r, &packet);
        RTMPPacket_Free(&packet);
    }

    RTMP_Close(r);
    RTMP_Free(r);

    return 1;
}

Here's a link to the log/backtrace. (As it's pretty long)

I'm unsure to why this is happening, is this a problem with my code or librtmp itself?


Solution

  • Using RTMPPacket_Alloc(packet, size); worked, although I've seen other code not using it (I think). Anyway here is a working example.

    #include <stdio.h>
    #include <stdlib.h>
    #include <librtmp/rtmp.h>
    #include <librtmp/log.h>
    
    int main(){
        RTMP *r;
        RTMPPacket packet;
        RTMPPacket_Alloc(&packet, 4096);
    
        char uri[] = "rtmp://167.114.171.21:1936/tinyconf app=tinyconf timeout=180000 live=1 conn=S:ROOMNAME swfurl=http://tinychat.com/embed/Tinychat-11.1-1.0.0.0602.swf";
    
        RTMP_LogLevel loglvl=RTMP_LOGDEBUG2;
        RTMP_LogSetLevel(loglvl);
    
        r = RTMP_Alloc();
        RTMP_Init(r);
        RTMP_SetupURL(r, (char*)uri);
        RTMP_Connect(r, NULL);
    
        while (RTMP_IsConnected(r)) {
            RTMP_ReadPacket(r, &packet);
            if (!RTMPPacket_IsReady(&packet))
                continue;
            RTMP_ClientPacket(r, &packet);
            RTMPPacket_Free(&packet);
        }
    
        RTMP_Close(r);
        RTMP_Free(r);
    
        return 1;
    }