| |

VerySource

 Forgot password?
 Register
Search
View: 3280|Reply: 11

Ask: the issue of releasing memory

[Copy link]

1

Threads

4

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2021-3-15 12:30:01
| Show all posts |Read mode
Ask: the issue of releasing memory

Hello everyone, I need to open up a memory buffer in my program. After I open it up and use it, I want to release this memory buffer after closing the device and use the following code:
        if (pData!=NULL) //pData is the memory buffer I developed and used
        {
            delete pData;
        }
        pData=NULL;
When this code is not added, the program is normal, but after adding it, the compilation is passed, the link is passed, and a dialog box pops up as soon as the program is run:

Debug Assertion Failed!
Program:D:\test.exe
File:dbgheap.c
Line: 1011

Expression :_CrtIsValidHeapPointer (pUserData)

For information on how........

Terminate Retry Ignore


This is the first time I have encountered such a problem. I don't know how it happened. Please help. Thank you
Reply

Use magic Report

0

Threads

10

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

Post time: 2021-3-15 12:45:02
| Show all posts
Pointer is invalid ................
Take the code at the time of the application
Reply

Use magic Report

0

Threads

70

Posts

42.00

Credits

Newbie

Rank: 1

Credits
42.00

 China

Post time: 2021-3-15 13:00:01
| Show all posts
Change it to look like the following. In addition, can you guarantee that the memory is valid when pData != NULL?

if (pData!=NULL) //pData is the memory buffer I developed and used
{
   delete []pData;
}
pData=NULL;
Reply

Use magic Report

1

Threads

4

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

 Author| Post time: 2021-3-15 13:15:01
| Show all posts
Okay, the code is as follows:

void CAd65_testDlg::OnOpenDevice()
{
    // TODO: Add your control notification handler code here
   
    hDevice=Fmad_OpenDevice(0);
    if(hDevice!=INVALID_HANDLE_VALUE)
    {
      m_StatusDisplay.Format("Open successfully!\n");
      UpdateData(FALSE);
         
        GetDlgItem(IDC_OpenDevice)->EnableWindow(TRUE);
        GetDlgItem(IDC_StartSample)->EnableWindow(TRUE);
        GetDlgItem(IDC_CloseDevice)->EnableWindow(TRUE);
        UpdateData(FALSE);

   
    }
    else
    {
    m_StatusDisplay.Format("Failed to open!\n");
      UpdateData(FALSE);
    }
}



//------------------------------------------------ ---------------------------------//

void CAd65_testDlg::OnStartSample()
{
    // TODO: Add your control notification handler code here
    BOOL bStatus=FALSE;

    PULONG pData=new ULONG[786432]; //Apply for memory! ! ! ! ! ! ! ! ! ! !
    memset(pData,0,3145728); //clear all to 0
    int n = 0;


  bStatus=Fmad_WriteN(hDevice, 20);


  bStatus = Fmad_WriteUserParameter(hDevice, 62, 255);

                  
  bStatus = Fmad_SelClk(hDevice, FALSE);

               
  bStatus = Fmad_SetSample(hDevice, 0x3);


   
  bStatus = Fmad_StartSample(hDevice);


  bStatus = Fmad_WriteTrigger(hDevice);
  

    DWORD begin = 0;
    DWORD end = 0;
   
  
begin = GetTickCount();
cycle: while (!Fmad_DataReady(hDevice))
    {
    }
         
    if((n==0)||(n%2==0))
    {
    bStatus=Fmad_GetTrigData(hDevice,pData+(n*0x2000),0x4000*2,0x0);
           n++;
    }
    else
    {
    bStatus=Fmad_GetTrigData(hDevice,pData+(n*0x2000),0x4000*2,0x8000);
    n++;
    }

         if(n <64)
    {
    goto cycle;
    }

    bStatus = false;
    bStatus = Fmad_SetSample(hDevice, 0x0);
   
    end = GetTickCount();
    DWORD timeUse = end-begin; //The number of milliseconds used for execution
            
    static char BASED_CODE szFilter[] = "Binary file (*.dat)|*.dat||";
    CString msg;
    CString sFile;
    CFileDialog dlgFile(FALSE,NULL,NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,\
                        szFilter,NULL);
    dlgFile.m_ofn.lpstrTitle="Save";
    dlgFile.DoModal();
    sFile=dlgFile.GetFileName();
    if(!sFile.IsEmpty())
    {
        int n=sFile.Find(".dat");
        if(n<0)
            sFile=sFile+".dat";
        CFile file;
        if(!file.Open(sFile,CFile::modeCreate|CFile::modeWrite))
        {
            msg.Format("Can't not open the file %s.",sFile);
            AfxMessageBox(msg);
            return;
        }

     file.Write(pData,3145728);
     file.Close();
/*
     if (pData!=NULL) //pData is the memory buffer I opened and used, as long as it is added, the program will pop up a dialog box
        {
          delete []pData;
        }
*/
}
}

void CAd65_testDlg::OnCloseDevice()
{
    // TODO: Add your control notification handler code here
          hDevice = Fmad_CloseDevice(hDevice);
            if(hDevice==INVALID_HANDLE_VALUE)
    {
        GetDlgItem(IDC_OpenDevice)->EnableWindow(TRUE);
        GetDlgItem(IDC_StartSample)->EnableWindow(FALSE);
        GetDlgItem(IDC_CloseDevice)->EnableWindow(FALSE);
    }
    else
        AfxMessageBox("Can't Close Device !");
}
Reply

Use magic Report

0

Threads

10

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

Post time: 2021-3-15 13:30:01
| Show all posts
As said on the second floor

delete []pData;
Reply

Use magic Report

0

Threads

70

Posts

42.00

Credits

Newbie

Rank: 1

Credits
42.00

 China

Post time: 2021-3-15 13:45:01
| Show all posts
PULONG pData=new ULONG[786432]; //Apply for memory! ! ! ! ! ! ! ! ! ! !
memset(pData,0,3145728); //Clear all 0 to 786432

I was wrong above
Reply

Use magic Report

0

Threads

10

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

Post time: 2021-3-15 14:00:01
| Show all posts
memset(pData,0,3145728);
-----------------------
Is this right
Reply

Use magic Report

0

Threads

70

Posts

42.00

Credits

Newbie

Rank: 1

Credits
42.00

 China

Post time: 2021-3-15 14:15:01
| Show all posts
delete [](ULONG*)pData;
Reply

Use magic Report

1

Threads

4

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

 Author| Post time: 2021-3-15 14:30:01
| Show all posts
Change it to this: delete [](ULONG*)pData;
Or not
memset(pData,0,3145728); Yes, it is to clear the byte to 0
Reply

Use magic Report

0

Threads

70

Posts

42.00

Credits

Newbie

Rank: 1

Credits
42.00

 China

Post time: 2021-3-15 14:45:01
| Show all posts
You block first

file.Write(pData,3145728);
file.Close();

Take a look at delete
I seem to have encountered this problem before, and I can’t remember why for a while
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list