.net - Pointers in Visual C++ Classes -
i updating program written else in c++/cli control variety of different cameras. program uses class control each camera type. class called zyladriver. new c++ , c++/cli, thank in advance , patience.
the sdk camera using has 1 function,at_queuebuffer(), allocate memory read data into. once image has been acquired, at_waitbuffer() can used return pointer acquired data.
i have 2 separate class functions call these functions, zyladriver::expose(), , zyladriver::readimage(uint16 * buffer).
however, running problems memory access going between functions. stands, code below produces system.accessviolationexception when attempt assigned strideless_buffer in zyladriver::readimage().
i believe problem possibly related deficiencies in understanding of how managed memory works in c++/cli. or perhaps more basic deficiencies in understanding of memory management in c++.
zyladriver.h:
#include "driver.h" #include "atcore.h" ref class zyladriver : driver { public: zyladriver(); ~zyladriver(); virtual void expose(string^ mode) override; virtual void readimage(uint16 *buffer, string^ mode) override; protected: unsigned char* testbufferptr; };
zyladriver.cpp
#include "zyladriver.h" #include "atcore.h" using namespace system::windows::forms; zyladriver::zyladriver(){}; zyladriver::~zyladriver(){}; void zyladriver::expose(string^ mode){ if (mode == "normal){ at_64 imagesizebytes; at_getint(*pcamera, l"imagesizebytes", &imagesizebytes); int buffersize = static_cast<int>(imagesizebytes); //create buffer(s) store acquisition. testbufferptr = new unsigned char[buffersize]; at_queuebuffer(*pcamera, testbufferptr, buffersize); at_command(*pcamera, l"acquisitionstart"); } } void zyladriver::readimage(uint16 *buffer, string^ mode){ //remove padding @ end of image rows, , handle 16bit deep zyla images. if (mode == "normal"){ at_64 imagesizebytes; at_getint(*pcamera, l"imagesizebytes", &imagesizebytes); int buffersize = static_cast<int>(imagesizebytes); unsigned char * bufferpointer; at_waitbuffer(*pcamera, &bufferpointer, &buffersize, at_infinite); int rows = getrows(); int cols = getcols(); int imagesize = static_cast<int>(imagesizebytes); at_64 bytesperrow; at_getint(*pcamera, l"aoistride", &bytesperrow); int stride_bytes = static_cast<int>(bytesperrow)-2 * rows; //remove useless data added camera unsigned char * strideless_buffer = new unsigned char[2 * rows*cols]; (int y = 0; y < rows; y++){ (int x = 0; x < 2 * cols; x++){ strideless_buffer[x + y * 2 * cols] = bufferpointer[x + y*bytesperrow]; } } //converting array of 1-byte data 2-byte pixels. uint16* imagebuffer_int = new uint16[rows*cols]; memcpy(imagebuffer_int, strideless_buffer, 2 * rows*cols); //simplicio assumes row counting starts @ top, andor software assumes //it starts @ bottom. need flip image here consistancy. (int y = 0; y < rows; y++){ (int x = 0; x < cols; x++){ buffer[x + y*cols] = imagebuffer_int[x + (rows - y - 1)*cols]; } } delete[] strideless_buffer; delete[] imagebuffer_int;
please let me know if can clarify or add information.
i can create class member pointer (which tried first), can moved around gc. at_queuebuffer doesn't know that. when try access buffer class function, find uninitialized memory. second attempt use pin_ptr, these cannot class members.
that regarding piece of code:
//create buffer store acquired data. unsigned char * userbuffer = new unsigned char[buffersize];
now i'm not sure how picture c++/cli work, that's unmanaged block of memory. never "move around". not "deallocated after expose() finishes".
i don't doubt may have problem, analysis wrong. perhaps post actual working example demonstrates instead of trying guess is.
Comments
Post a Comment