I am trying to generate a RSA private key and keep it in a array as well as write into a der file. However, I get segmentation fault (core dumped). When I use valgrind to check the memory, it points to wc_InitRng and wc_FreeRng functions. Please help me with this. Thanks.
The leak information is : 
==16570== Invalid write of size 8
==16570==    at 0x4E40BE4: wc_InitRsaKey (in /usr/local/lib/libwolfssl.so.0.0.2)
==16570==    by 0x400EF5: RSA_genkey(char const*, unsigned char*, unsigned long) (common.cpp:34)
==16570==    by 0x400E25: main (master.cpp:22)
==16570==  Address 0xfff001458 is not stack'd, malloc'd or (recently) free'd
==16570==
==16570==
==16570== Process terminating with default action of signal 11 (SIGSEGV)
==16570==  Access not within mapped region at address 0xFFF001458
==16570==    at 0x4E40BE4: wc_InitRsaKey (in /usr/local/lib/libwolfssl.so.0.0.2)
==16570==    by 0x400EF5: RSA_genkey(char const*, unsigned char*, unsigned long) (common.cpp:34)
==16570==    by 0x400E25: main (master.cpp:22)
==16570==  If you believe this happened as a result of a stack
==16570==  overflow in your program's main thread (unlikely but
==16570==  possible), you can try to increase the size of the
==16570==  main thread stack using the --main-stacksize= flag.
==16570==  The main thread stack size used in this run was 8388608.
==16570==
==16570== HEAP SUMMARY:
==16570==     in use at exit: 0 bytes in 0 blocks
==16570==   total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==16570==
==16570== All heap blocks were freed -- no leaks are possible
==16570==
==16570== For counts of detected and suppressed errors, rerun with: -v
==16570== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
Segmentation fault (core dumped)
#define RSA_KEY_SIZE 2048
#define EXPONENT 65537
int RSA_genkey( const char * file_der, unsigned char *der, size_t der_len){
    RsaKey priv;
    RNG rng;
    FILE*  keyFile = NULL;
    int ret = 0, der_written = 0;
    if ( ( ret = wc_InitRsaKey( &priv, NULL ) ) != 0 )
    {
        printf(" RSA_genkey failed in wc_InitRsaKey: returned %d\n", ret);
        goto exit;
    }
    if ( ( ret = wc_InitRng( &rng ) ) != 0 )
    {
        printf(" RSA_genkey failed in wc_InitRng: returned %d\n", ret);
        goto exit;
    }
    if ( ( ret = wc_MakeRsaKey( &priv, RSA_KEY_SIZE, EXPONENT, &rng ) ) != 0 )
    {
        printf(" RSA_genkey failed in wc_MakeRsaKey: returned %d\n", ret);
        goto exit;
    }
    if ( ( der_written = wc_RsaKeyToDer( &priv, (byte*)der, der_len) ) <= 0 )
    {
        printf(" RSA_genkey failed in wc_RsaKeyToDer: returned %d\n", der_written);
        wc_FreeRsaKey( &priv );
        wc_FreeRng( &rng );
        return der_written;
    }
    if ( (keyFile = fopen( file_der, "wb" ) ) == NULL )
    {
        printf(" RSA_genkey failed when opening file.\n");
        ret = -1;
        goto exit;
    }
    if ( fwrite( der, 1, der_written, keyFile) != (size_t)der_written )
    {
        printf(" RSA_genkey failed when writing into file.\n");
        fclose( keyFile );
        ret = -1;
        goto exit;
    }
    fclose( keyFile );
exit:
    wc_FreeRng( &rng );
    wc_FreeRsaKey( &priv );
    return ret;
}
int main(int , char const **)
{
    //int ret;
    unsigned char der[4096];
    memset (der, 0, sizeof(der) );
    RSA_genkey("./rsa_priv.der", der, sizeof(der) );
    return 0;
}