Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6323015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T16:26:21+00:00 2026-05-24T16:26:21+00:00

I am using C++ and Visual Studio 2005. I have a project that memory

  • 0

I am using C++ and Visual Studio 2005.

I have a project that memory Increases in a very abnormal. When debug the code I Realized That there are Several parts That Contribute to it. Like this for example:

 // has to add crypt32.lib to link
#include <windows.h>
#define MY_ENCODING_TYPE  (PKCS_7_ASN_ENCODING | X509_ASN_ENCODING)

void memoryUP( const unsigned char *pData, int cData )
{
    HCRYPTMSG  msg  = NULL;
    HCRYPTPROV hProv        = NULL;

    CryptAcquireContext(&hProv,NULL,NULL,PROV_RSA_FULL,0);

    msg = CryptMsgOpenToDecode(MY_ENCODING_TYPE,0,0,hProv,NULL,NULL); 

    if(!(CryptMsgUpdate( msg, pData, cData, TRUE)))
    {
        if(msg != NULL)
        {
            CryptMsgClose(msg);
            msg = NULL;
        }
    }

    if (hProv != NULL)
        CryptReleaseContext(hProv,0);

    if (msg != NULL)
    {
        CryptMsgClose(msg);
        msg = NULL;
    }
}

int main(int argc, char** argv)
{
    MyFile myfile = myReadFile("c:\\file.p7s");

    {
        for(int i=0; i<100000; ++i)
        {
            memoryUP( myfile._data, myfile._length );
        }
    }

    delete myfile;

    return 0;
}

When I run this code, the memory goes up continuously “when call CryptMsgUpdate”. Am I deallocating wrong?

I tried to use Memory Leak Detection Enabling method to detect memory leak but nothing appears:

#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>

and

_CrtDumpMemoryLeaks();

Thanks in Advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T16:26:23+00:00Added an answer on May 24, 2026 at 4:26 pm

    You have to release resources in reverse order of their acquisition:

     CryptAcquireContext();
    
     if (success)
     {
       CryptMsgOpenToDecode();
    
       if (success)
       {
          CryptMsgClose();
       }
       // else: nothing to close, opening failed
    
       CryptReleaseContext();
     }
     // else: nothing to release, acquisition failed
    

    The deeper nested constructions depend on the outer ones and may lock up resources, so you can only release the prerequisite resources after you’ve released the dependent ones.

    Since you tagged this C++, I would be remiss to mention that those sort of things should be handled with RIAA, and you should make a class that takes responsibility for the resource. As you can see even in this simple example, writing the correct error checking paths very quickly become onerous, so it’d be much better and more modular to have a class that cleans up after itself, which automatically happens in the correct order.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am creating windows installer project using Visual Studio 2005. Is there an option
I'm using Visual Studio 2005. I have a program written in C#. When I
I have Visual Studio 2005 Professional ENU installed and want to create GUIDs using
I am using DSL Tools for Visual Studio 2005. I have a DSL where
Does anyone have experience using makefiles for Visual Studio C++ builds (under VS 2005)
I have been using Visual Studio 2005 under Windows XP Pro 64-bit for C
I'm using Visual Studio 2005 on a Windows XP SP3 machine and recently have
I currently have a project I am building in Visual Studio 2005 and I
I'm using Visual Studio 2005 to script out a database so that I can
I am using Visual Studio 2005 with Team Foundation Server. When I right click

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.