# COM reference problem



## Chris-Z (May 26, 2016)

Hello,
I use office OBS to develop some plugin to record video,but face the com reference problem, some code like below(some code from official version, so you may familier with it)

.cpp

if (!InitGlobalConfig())
        return false;

    char path[512];

    if (GetConfigPath(path, sizeof(path), "obs-studio/plugin_config") <= 0)
        return false;

    if (!obs_startup("zh-CN", path, NULL))
        return false;

    //curl_global_init(CURL_GLOBAL_ALL); ?
    char configPath[512];

    int ret = GetProfilePath(configPath, sizeof(configPath), "");
    if (ret <= 0) {
        return false;
    }

    if (os_mkdir(configPath) == MKDIR_ERROR) {
        return false;
    }

    ret = GetProfilePath(configPath, sizeof(configPath), "basic.ini");
    if (ret <= 0) {
        return false;
    }

    int code = basicConfig.Open(configPath, CONFIG_OPEN_ALWAYS);
    if (code != CONFIG_SUCCESS) {
        return false;
    }

    InitBasicConfigDefaults();

    AddExtraModulePaths();
    obs_load_all_modules();

    ResetOutputs();

    // create default scene and item
    m_Scene = obs_scene_create("Basic.Scene");
    if (m_Scene == NULL)
        return false;

    if (!HasAudioDevices(OUTPUT_AUDIO_SOURCE))
        return false;

    if (!ResetAudio())
        return false;
    if (ResetVideo() != 0)
        return false;

    ResetAudioDevice(OUTPUT_AUDIO_SOURCE, "default",
            "Basic.DesktopDevice1", 1);

    InitDefaultTransitions();

    const char *type;
    bool bFoundRecordSource = false;
    size_t idx = 0;

    while (obs_enum_input_types(idx++, &type))
    {
        const char *name = obs_source_get_display_name(type);

        if (strcmp(type, "scene") == 0)
            continue;

#ifdef _DEBUG
        const  char name2[128] = {0};
        memcpy((void*)name2, name, strlen(name));

        // NOTE: do not use name directly, UTF8ToEncode will free it
        char* what = UTF8ToEncode(name2);

        blog(LOG_DEBUG, "name is: %s type is: %s", what, type);
        delete what;
#endif
        const char* displayName = obs_source_get_display_name(type);

        /* possible value
        image_source
        ffmpeg_source
        text_ft2_source
        monitor_capture
        window_capture
        game_capture
        dshow_input
        wasapi_input_capture
        wasapi_output_capture      
        */

        if (strcmp(type, "monitor_capture") == 0)
        {
            // 1. create video input source
            m_videoInputSource = obs_source_create(type, displayName, NULL, nullptr);

            if (m_videoInputSource == NULL)
                continue;

            // add to scene
            AddSourceData data;
            data.source = m_videoInputSource;
            data.visible = true;

           // with this line, when shut down obs, it throw an obs_source_release problem, why
            obs_scene_atomic_update(m_Scene, AddSource, &data);

            // 3. get default setting
            m_videoInputsettings = obs_source_get_settings(m_videoInputSource);

            // 4. get default properties
            obs_properties_t* properties = obs_source_properties(m_videoInputSource);

#ifdef _DEBUG
            // 5. enum properties, to know what properties exist
            obs_property_t *property = obs_properties_first(properties);
            while (property)
            {
                AddProperty(property);
                obs_property_next(&property);
            }

#endif

            //obs_source_release(inputsource);
            bFoundRecordSource = true;

            break;
        }


        // not exists unless you create it DO NOT USE
        //obs_source_t *source = nullptr;
        //source = obs_get_source_by_name(type);
        //while ((source = obs_get_source_by_name(name)) )
        //{
        //    obs_source_release(source);
        //}

    }

    //obs_enum_sources(EnumSources, NULL);
    //obs_scene_release(scene);

    if (!bFoundRecordSource)
        return false;

    StartRecording();

.h file

```
class TestRecordMonitor
{
    ConfigFile                     globalConfig;
    ConfigFile                       basicConfig;
    TextLookup                     textLookup;
    OBSContext                     obsContext;

    OBSService                        service;// streaming service
    std::unique_ptr<BasicOutputHandler> outputHandler;

    OBSSource                        m_videoInputSource;// com / AddRef / ReRef
    OBSScene                        m_Scene;
    OBSData                         m_videoInputsettings;
```


----------



## Chris-Z (May 26, 2016)

oh, sorry, I hightlight the code ,but it not works

 // with this line, when shut down obs, it throw an obs_source_release problem, why
 obs_scene_atomic_update(m_Scene, AddSource, &data);


----------

