Am I using the flush_rewrite_rules function in the right place?

Considering WordPress Codex flush_rewrite_rules

This function is useful when used with custom post types as it allows for automatic flushing of the WordPress rewrite rules (usually needs to be done manually for new custom post types). However, this is an expensive operation so it should only be used when absolutely necessary.

I’d recommend you to use it in this ways:

Example-1. ONLY DEVELOPMENT! Flush with get variable:

if(isset($_GET['secret_variable'])){
   flush_rewrite_rules();
}

Then simply call http://foo.com/?secret_variable=1 to flush it.

Example-2. Declaring new post_type and flushing rules on WordPress theme switch action

function custom_post_type_function(){
    $args = array();
    register_post_type('custom_post_type', $args);
}

function custom_flush_rules(){
    flush_rewrite_rules();
}
add_action('after_theme_switch', 'custom_flush_rules');
add_action('init', 'custom_post_type_function');

Also, this [Where, When, & How to Properly Flush Rewrite Rules Within the Scope of a Plugin?] may helps for better understanding