From: tomdickens on
I have a Windows Forms application, Visual C++ 2005, compiling it using /clr.
I need to use the image-handling library CImg, which is packaged simply as a
header file CImg.h that you include in your project.

I added CImg.h to stdafx.h, inside #pragma unmanaged/managed preprocessor
instructions. The code compiles just fine.

However, when I construct a CImg object and then try to write text into it
to display, I get a memory error -
System.AccessViolationException: Attempted to read or write protected
memory. This is often an indication that other memory is corrupt.
at cimg_library.CImg<unsigned char>.draw_text<unsigned char>(CImg<unsigned
char>* , Int32 , Int32 , SByte* , Byte* , Int32 , Single , UInt32 )

Is this because of using unmanaged memory in a managed code? I thought IJW
would handle this. I've asked this same question on the CImg forum but have
no reszponse yet.


Thanks,
Tom

Here is the detailed code:

Put in stdafx.h:
#pragma once
#pragma unmanaged
#pragma comment (lib, "shell32.lib")
#pragma comment (lib, "gdi32.lib")
#pragma comment (lib, "user32.lib")
#define cimg_OS 2
#define cimg_display_type 2
#include "CImg.h"
using namespace cimg_library;
#pragma managed

Call the functions:
CImg<unsigned char> img(640,400,1,3); // Define a 640x400 color image with 8
bits per color component.
img.fill(0); // Set pixel values to 0 (color : black)
unsigned char purple[] = { 255,0,255 }; // Define a purple color
img.draw_text(100,100,"Hello World",purple); // IT DIES HERE ---- Draw a
purple "Hello world" at coordinates (100,100).
img.display("My first CImg code"); // Display the image in a display window.
From: David Lowndes on
>Is this because of using unmanaged memory in a managed code?

Tom,

I'm not familiar with CImg, but what happens if you use the same
source code in a native application?

Dave
From: David Wilkinson on
tomdickens wrote:
> I have a Windows Forms application, Visual C++ 2005, compiling it using /clr.
> I need to use the image-handling library CImg, which is packaged simply as a
> header file CImg.h that you include in your project.
>
> I added CImg.h to stdafx.h, inside #pragma unmanaged/managed preprocessor
> instructions. The code compiles just fine.
>
> However, when I construct a CImg object and then try to write text into it
> to display, I get a memory error -
> System.AccessViolationException: Attempted to read or write protected
> memory. This is often an indication that other memory is corrupt.
> at cimg_library.CImg<unsigned char>.draw_text<unsigned char>(CImg<unsigned
> char>* , Int32 , Int32 , SByte* , Byte* , Int32 , Single , UInt32 )
>
> Is this because of using unmanaged memory in a managed code? I thought IJW
> would handle this. I've asked this same question on the CImg forum but have
> no reszponse yet.
>
>
> Thanks,
> Tom
>
> Here is the detailed code:
>
> Put in stdafx.h:
> #pragma once
> #pragma unmanaged
> #pragma comment (lib, "shell32.lib")
> #pragma comment (lib, "gdi32.lib")
> #pragma comment (lib, "user32.lib")
> #define cimg_OS 2
> #define cimg_display_type 2
> #include "CImg.h"
> using namespace cimg_library;
> #pragma managed
>
> Call the functions:
> CImg<unsigned char> img(640,400,1,3); // Define a 640x400 color image with 8
> bits per color component.
> img.fill(0); // Set pixel values to 0 (color : black)
> unsigned char purple[] = { 255,0,255 }; // Define a purple color
> img.draw_text(100,100,"Hello World",purple); // IT DIES HERE ---- Draw a
> purple "Hello world" at coordinates (100,100).
> img.display("My first CImg code"); // Display the image in a display window.

What is the precise C++ declaration of the method, and how are you calling it?

--
David Wilkinson
Visual C++ MVP
From: tomdickens on
The same code works fine in an MFC application.

CImg is packaged as a very large header file, CImg.h. It's a C++
Template-based image-handling class that a lot of people seem to use. (I
posted this question on their forum but haven't gotten a reply.)

I could post the CImg.h parts that are required here, but I don't know if
that would be productive. Probably the most important part is that the image
memory is allocated using 'new.' Since this worked in MFC, I'm really
thinking that the managed/unmanaged interop is not working here.

I'm fairly new to trying to use managed and unmanaged code together, it has
given me loads of trouble!

Thanks,
Tom

"David Lowndes" wrote:

> >Is this because of using unmanaged memory in a managed code?
>
> Tom,
>
> I'm not familiar with CImg, but what happens if you use the same
> source code in a native application?
>
> Dave
> .
>
From: David Lowndes on
>The same code works fine in an MFC application.

OK. Is the code absolutely correct though? i.e. is the native version
working partly by chance/circumstance?

>I could post the CImg.h parts that are required here, but I don't know if
>that would be productive.

Unlikely.

> Probably the most important part is that the image
>memory is allocated using 'new.' Since this worked in MFC, I'm really
>thinking that the managed/unmanaged interop is not working here.

Can you show just the relevant part of the code (if you think it would
make sense to someone not familiar with using CImg)?

Dave