CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 2011
    Posts
    1

    Unhappy IntPtr causing memory leak?

    this function is in a loop, and when i run the program, the line with intptr is giving me memory problems, 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;
    }

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: IntPtr causing memory leak?

    Your code is not a VC++ nor a native C++.
    Try to ask in the Managed C++/CLI forum
    Victor Nijegorodov

  3. #3
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: IntPtr causing memory leak?

    Victor is right that you're in the wrong section but maybe we can get this one sorted out quickly.

    Keep in mind that, though the name suggests something else, IntPtr is no pointer! It's just an integral type with the machine's word size, IOW pointer size. (Maybe the should've better named it PtrInt.) Therefore, this doesn't make sense and your memory leak is no surprise:

    Quote Originally Posted by Qmage View Post
    Code:
    delete[] ip;
    I wonder why this didn't raise a compilation error in the first place. There is a conversion from IntPtr to void *, but according to MSDN it's explicit. Try this instead:

    Code:
    delete[] ip.ToPointer();
    I didn't check the commented-out portions of your code.

    In case this doesn't solve the problem and/or you have more questions about C++/CLI, please post them in the appropriate section.

    Please use code tags when posting code.

    Ah, and... Welcome to CodeGuru!
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured