|
Hi,
Sorry. I was just writing "thread finished" as a test in the program. I should have written "thread called" or something similar. I just wanted to see if the thread would work independantly of the main program/thread.
You say that you don't see anything wrong with the code. Do that mean that when a thread finished that it reallocates any memory from on the heap as free to use memory?
Also you said << Why HeapAlloc and not "new"? >>. Why do you mean?
thanks for your reply,
Y
|
|
|
|
|
Ylno wrote: Do that mean that when a thread finished that it reallocates any memory from on the heap as free to use memory?
No, I said there's a memory leak. I don't see any HeapFree() call
releasing the memory.
Ylno wrote: you said << Why HeapAlloc and not "new"? >>. Why do you mean?
I'm just curious why you're directly calling Win32 memory management
functions instead of using the C++ "new" operator.
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Hi Mark,
I will put a heap free call in. Thanks.
|
|
|
|
|
Ok,
I've implemented a heap clean up at the end of the code. Problem now is that when i call this function it waits untill the thread finishes until returning.
Can anyone see a way of perhaps having the code below return the address of the Thread and the pPulseData and have another code clean the memory for these two addresses.
Thanks
float LPTPulseThreaded(float aPortAddress, float aValue1, float aValue2, float aTime1, float aTime2, float aTimes, float aControlAddress, float aOffOnValue){
PDATA pPulseData[1];
DWORD dwThread[1];
HANDLE hThread[1];
pPulseData[0] = (PDATA)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(DATA));
pPulseData[0]->PortAddress = aPortAddress;
pPulseData[0]->Value1 = aValue1;
pPulseData[0]->Value2 = aValue2;
pPulseData[0]->Time1 = aTime1;
pPulseData[0]->Time2 = aTime2;
pPulseData[0]->Times = aTimes;
pPulseData[0]->ControlAddress = aControlAddress;
pPulseData[0]->OffOnValue = aOffOnValue;
hThread[0] = CreateThread(NULL, 0,PulseThread, pPulseData, 0, &dwThread[0]);
if(hThread[0] == NULL){return 1;}
WaitForMultipleObjects(1, hThread, TRUE, 10000);
HeapFree(GetProcessHeap(), 0, pPulseData[0]);
pPulseData[0] = NULL;
return 0;
}
|
|
|
|
|
Ylno wrote: when i call this function it waits untill the thread finishes until returning.
Then there's no reason to use a separate thread.
You've just functionally created an overly complicated single thread.
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Mark,
I know i've created a useless piece of code. Please give me a hint about how to make this piece of code fit what i'm trying to do...
How do I make sure the heap is freed up and let this function return so that the scripting language can continue its excecution.
En espérant
|
|
|
|
|
Assuming the DLL and the scripting language need no further interaction
after the call...
DLLFunctionCalledByScriptingLanguage
{
createthread(ThreadProc)
return
}
ThreadProc
{
allocate some memory
do some stuff
free memory
return
}
That's the same as yours except the memory operations are all
in the thread procedure and the function doesn't wait for the thread
to end before returning control to the caller, thus making it truly
multithreaded. Make sense?
How often and how many times would this be called by the scripting language?
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Hello,
i am working on a 3D project. some where in my program i need to draw a plan that is perpendicular to a vector MB. for doing this i need to know the (x,y,z) of each voxel in this plan (a portion of the plan or window).
for this i need to make a marix that transform a well known vector MA (for exemple parallel to X axis) to a known vector MB of the same length. why ? because I will calculate the (x,y,z) of the plan perpendicular to X axis (witch is easy to calculate) and then multiplay by the matrix to obtain the (x,y,z) in the new plan (perpendicular to MB)
So i need a rotation matrix (because of the same origin M of the two vectors). I think i can do only two rotations (on Y and Z axis) to obtain the new vector MB.
think you
|
|
|
|
|
See, for instance [^].
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
|
In the Matrix literature I only find rotation about x, y or z axis or even rotation about a vector. but i can t find a matrix to have a vector from an other.
I don t know if i can consider the multiplication of two matrix (one alpha rotation about y and beta rotation about z) and try to find alpha and beta.
excuse me but i am not good at maths
|
|
|
|
|
Well, you may obtain a vector from another (having the same lenght) simply translating + rotating it.
Off course you need to find the translation and rotation parameters.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
I have found the solution (no translation is needed because of the same origine)
To find a matrix that transform a vector MA to a vector MB you can try this:
1) If MA and MB are not unit length, normalize it.
2) Calculate the dot product of MA and MB, this will give the cosinus of the angle between MA and MB. To get the angle, take the arccos of the result.
3) Calculate the cross product of MA and MB, this will give a vector for the axe of rotation.
4) Now we have an angle and a vector. Then you can use the Rodrigues formula to calculate a rotation matrix to rotate MA to MB.
|
|
|
|
|
Balanced here.
Many are stubborn in pursuit of the path they have chosen, few in pursuit of the goal - Friedrich Nietzsche
.·´¯`·->Rajesh<-·´¯`·.
[Microsoft MVP - Visual C++]
|
|
|
|
|
Thank you, pal.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
I'm not sure how I should go about approaching this problem, but is there any way to create a bitmap by loading a "file in memory"?
Let me explain. So, I initially have an encrypted .bmp on disk, that I load into a std::ifstream, decrypt, and dump back out to a char*. The char array essentially contains the entire file, character by character, meaning that if I were to dump it to an std::ofstream, I would get the decrypted version of the file I loaded into the std::ifstream (which would also happen to be a bitmap that I can view with any picture viewer). The problem is, I want to generate a bitmap from this "file in memory" stored as a char* and all the functions I know of either require a file name, or require loading it from an executable resource. The end result should be a Win32 BITMAP object. Is there any way of doing what I'd like to do?
Thanks.
|
|
|
|
|
|
This might work. I'm not inherently familiar with some of the parameters that have to be passed in, but I think I'll manage (and post back if I have issues).
Thanks again!
|
|
|
|
|
Cyrilix wrote: I'm not inherently familiar with some of the parameters that have to be passed in
Here's a clue...
The first structure is a BITMAPFILEHEADER[^].
You'll find an offset to the pixel data there.
Following that is a BITMAPINFO[^] structure,
within which you'll find a BITMAPINFOHEADER[^]
structure.
Bitmap Storage[^]
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
I've been looking around for code examples and trying to figure out the concepts in my head, and somehow, I think I'm using the wrong function (CreateDIBitmap) for what I want to do, since I have to pass in a device context. From what I've read, the device context is like a drawing surface, but I have no need for a drawing surface, since I don't actually plan on drawing the bitmap to a window. All I want to do is get a BITMAP which can then have its pixels altered (which in my mind, would mean setting bits contained in my BITMAP.bmBits structure to different values). What do you guys think? Am I just really confused or onto something?
To clarify, my plan was the following:
-Load encrypted file and store it as an encrypted memory stream
-Decrypt memory stream and convert to BITMAP type
-Do image processing on BITMAP type
-Convert BITMAP back to a decrypted memory stream and encrypt
-Save encrypted memory stream to file
|
|
|
|
|
If you're going to manipulate the pixel data directly, then
you can use a DIBSection ("a DIB that applications can
write to directly").
CreateDIBSection()[^]
The passed DC can be NULL for DIB_RGB_COLORS bitmaps.
If you don't use a file mapping to create the DIBSection, you'll need
to copy the pixel bits from your array to the allocated pixel bits pointer.
The number of bytes to copy can be determined something like the following
(psudocode):
LONG NumberOfPixelBytes;
if (0 != BITMAPINFOHEADER.biSizeImage)
NumberOfPixelBytes = BITMAPINFOHEADER.biSizeImage;
else
NumberOfPixelBytes = (((BITMAPINFOHEADER.biWidth * (long)BITMAPINFOHEADER.biBitCount + 31L) & (~31L)) / 8L) * BITMAPINFOHEADER.biHeight;
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Hi Mark,
I've got a good idea of how to do this when it comes to initially loading the Bitmap. The only other issue I have is saving the Bitmap back. My impression is that from an HBITMAP that is returned from CreateDIBSection(), we can get a BITMAP by calling GetObject() in such a manner:
HBITMAP hBmp = CreateDIBSection(...);
BITMAP bmp;
GetObject(hBmp, sizeof(bmp), &bmp);
But, the BITMAP object itself does not seem to have enough information to recreate the BITMAP file. For instance, from the MSDN documentation, it states that BITMAP has the following members:
LONG bmType;
LONG bmWidth;
LONG bmHeight;
LONG bmWidthBytes;
WORD bmPlanes;
WORD bmBitsPixel;
LPVOID bmBits;
The BITMAP file specification, on the other hand, has (as your link described):
BITMAPFILEHEADER:
WORD bfType;
DWORD bfSize;
WORD bfReserved1;
WORD bfReserved2;
DWORD bfOffBits;
BITMAPINFO:
RGBQUAD bmiColors[1];
BITMAPINFOHEADER (inside BITMAPINFO):
DWORD biSize;
LONG biWidth;
LONG biHeight;
WORD biPlanes;
WORD biBitCount;
DWORD biCompression;
DWORD biSizeImage;
LONG biXPelsPerMeter;
LONG biYPelsPerMeter;
DWORD biClrUsed;
DWORD biClrImportant;
RGBQUAD array: data that has to do with palettes
Color-index array: the raw image data itself
If you compare the two lists, it would seem like BITMAP contains enough information to recreate most of the variables in BITMAPINFOHEADER as well as the info needed for the color-index array, but what about the rest of the info? It would seem like if I'm trying to convert it back to a char array, I would have to do some fancy extrapolation myself, in addition to saving some of the info that I had when I loaded the image. I will have another careful look at all of the BITMAP related functions to see if I've missed something, but at the moment, I'm sort of stuck at this point.
Also, if I haven't mentioned it to you yet, your advice so far been much appreciated.
|
|
|
|
|
Cyrilix wrote: from an HBITMAP that is returned from CreateDIBSection(), we can get a BITMAP by calling GetObject() in such a manner:
No need to make it that complicated - just keep the original headers!
All the info you need is there. You're only changing the pixels, right?
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Well, in a sense, that's true, but I'd also like to handle the case where I've gotten a BITMAP from somewhere else (i.e., just been passed a BITMAP from another function), and I'm asked to save it. Is that not possible to implement easily?
|
|
|
|
|
Cyrilix wrote: Is that not possible to implement easily?
BMP bitmaps are the easiest format to save. The Bitmap Storage[^]
shows the layout of the file.
Another option:
If you're not into writing the code yourself, and you can use an ATL
class, the CImage class[^] is a nice lightweight
wrapper around a DIBSecion bitmap and will load and save in other
formats beyond BMP. The member functions make it easy to do all
the things you've discussed so far.
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|