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

  • SEARCH
  • Home
  • 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 4267276
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T06:52:38+00:00 2026-05-21T06:52:38+00:00

This function is in a loop. When I run the program, the line with

  • 0

This function is in a loop. When I run the program, the line with IntPtr is giving me memory problems, I’ve put delete[], but it still doesn’t solve the memory problem, can anyone help please? thanks

void showImage(IplImage *img,System::Windows::Forms::PictureBox^ picturebox)
{

IntPtr ip(new unsigned char[img->widthStep*img->height]); // this line causing memory usage to keep going up very fast

//memcpy(ip.ToPointer(),img->imageData,img->widthStep*img->height);

//picturebox->Image = gcnew Bitmap(img->width,img->height, img->widthStep, System:rawing::Imaging::PixelFormat::Format24bppRgb, ip);

delete[] ip;
} 

This is C++\CLI

  • 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-21T06:52:39+00:00Added an answer on May 21, 2026 at 6:52 am

    It is rather sad that this code compiles, but that is by design. The delete operator applied to a managed type doesn’t actually free any memory. It calls the IDisposable::Dispose() method on the passed object. It is rather sad that this even works, the IntPtr gets boxed to turn it into an object and then checked to see if it implements the IDisposable interface. It doesn’t of course, nothing happens.

    You have to pass the pointer that you got back from the new operator. Don’t forget to do this in a finally block so an exception cannot cause a leak.

    Btw, there are more complications in the code that you commented. The Bitmap constructor you use requires you to keep the IntPtr valid, you cannot release the memory until the Bitmap is no longer used. So using delete isn’t actually valid. Consider using Bitmap.LockBits() instead to get a pointer to a Bitmap that manages its own memory. And watch out for stride.

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

Sidebar

Related Questions

This loop works fine in Main function but when copy this loop code in
So I do have this loop, but it stops when it come to function
I've got a function in Jython, this function uses Popen to run another program
The following code in my function (run in a loop) is causing my program
I wrote this function for filling closed loop, pixvali is declared globally to store
I have created this function to check abecedarian with while loop (A word is
I need to create a function that uses a loop. This function will open
No matter what I try I keep getting an infinite loop with this function:
I have a function creating entries via grid() like this: (replace while-loop with function,
I have a loop created with each, check this example: $('.foo').each(function(i){ //do stuff });

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.