|
My article has been updated with this information. Check it out.
"One must learn from the bite of the fire to leave it alone." - Native American Proverb
|
|
|
|
|
hi,
i'm passing a vector of doubles by reference to a function, which assigns some values to the vector. if i compile this function into a dll and link the library i get a run time debug assertion failure:
code to make library:
#include <vector>
using namespace std;
extern "C" __declspec(dllexport)void MyDblFunc(vector<double> &Dbl)
{
for(int i=0; i<10; i++)
{
Dbl.push_back(1.1*i);
}
return;
}
code to use the library function:
#include <vector>
#include <iostream>
#include <fstream>
using namespace std;
extern "C" __declspec(dllexport) void MyDblFunc(vector<double> &Dbl);
void main()
{
vector<double> MyDbl;
MyDblFunc(MyDbl);
int N= (int)MyDbl.size();
cout<<N<<'\n';
for(int i=0; i<N; i++)
{
cout<<MyDbl[i]<<'\n';
}
return;
}
which writes the expected output and then produces Debug Assertion Failures: _CrtIsValidHeapPointer(pUserData) and _pFirstBlock==pHead.
However, if i put the function body code in the source after main (ie not using a dll) then i get no such error. Why does this happen? and what can i do about it? i want to put as many of my subroutines as possible into dlls as they are common to many programs, and also i want to use vectors so that i can dynamically allocate memory at run time. Is there a difference between passing a vector by reference or passing a pointer to the vector... i have been told that the reference can get reallocated when the vector resizes which could cause a problem in the calling routine... any advice at all would be great.
Thanks for your help
|
|
|
|
|
i also read that 'STL classes are not safe for DLL's interfaces'. why?
|
|
|
|
|
This is not STL-specific problem. It happens because you allocate memory with one instance of CRT and then try to release it from another one.
Try one of the following two things:
1) Compile both the dll and exe with Multi-threaded Debug DLL (/MDd) option (in debug mode) so that both exe and dll share the same instance of CRT.
2) If you know in advance the size of the vector, reserve the memory for it before calling the dll function. This way, the same instance of CRT will allocate and release the memory:
void main()
{
vector<double> MyDbl(10);
MyDblFunc(MyDbl);
|
|
|
|
|
How do you measure a string with word wrap? I want to pass a text to the program, a Rectangle which to contain it in a given Bitmap, a starting font size, and I need the program to decrease the fontSize until the text fits the Rectangle. After the fontSize has been computed, the text will be drawn to the Bitmap using DrawString(...). However, I can't get MeasureString to compute the text limits. This is the code I use :
{
mFont = new Font(mFontFamily, mFontSize, UnitPixel);
DebugAssert(mFont != NULL);
FLOAT fontSize = mFontSize;
StringFormat *stringFormat = StringFormat::GenericTypographic()->Clone();
stringFormat->SetFormatFlags(0);
graphics.SetTextRenderingHint(TextRenderingHintAntiAlias);
RectF *textRectangle = new RectF(0, 0, 0, 0);
int codePointFitted;
int lines;
graphics.MeasureString(mTextString, mTextString.length(), mFont, layoutRectangle, stringFormat, textRectangle, &codePointFitted, &lines);
while (((textRectangle->Width > mMaxTextWidth) || (textRectangle->Height > mMaxTextHeight)) && (!(fontSize < 0)))
{
fontSize = fontSize - 1.0f;
mFont = new Font(mFontFamily, fontSize, UnitPixel);
DebugAssert(mFont != NULL);
graphics.MeasureString(mTextString, mTextString.length(), mFont, layoutRectangle, stringFormat, textRectangle);
}
if (fontSize < 5.0f)
fontSize = 5.0f;
mFont = new Font(mFontFamily, fontSize, UnitPixel);
DebugAssert(mFont != NULL);
delete stringFormat;
}
If I don't use a StringFormat the returned limits are 0.0, if I use it, it computes the limits exactly as those of the layoutRectangle. fontSize starts at about 120, and the limits of the layoutRectangle are (Width 180; Height 160), and the Text is long enough not to enter those limits. "HELLO WORLD" is long enough. Any help is appreciated .
|
|
|
|
|
Hello
I'm drawing some text on a texture. Now I would like to only draw the outline of the text in a different color. I use the D3DXCreateFont function and m_pFont->DrawText to display the text. Unfortunately, I couldn't find a way to get the outline of my text...
I read that creating a D3DText mesh object is very resource consuming, so I didn't want to do that...
Anybody can give me a hint?
|
|
|
|
|
I develop an application which adds the icon to system tray.
System tray icon contains menu items like.,1.start 2.shutdown.
>It contains min and close buttons
(dialog based appli).
>I dont want to terminate the application .,when I press close('X') button .It has to be hide .
When I press Shutdown in menu item it has to be destroyed.
Plz Give code , which is added in OnSysCommand
praveen Chowdam Kumar
|
|
|
|
|
HOW MANY TIMES WILL YOU ASK THIS FOR ?
did you ever seen what i told you in you previous thread ? if it doesn't help, just tell me, because i won't guess !
TOXCCT >>> GEII power [toxcct][VisualCalc]
|
|
|
|
|
what does the MapViewOfFile exactly do?
i have used the createFileMapping function for a text file.
I would want to know what the MapViewOfFile exactly does?does it copy the contents of the file to the object in the memory or just return the base address of the object in memory?
|
|
|
|
|
It doesn't copy any data, it just maps the file into your process address space for you to access as a byte array. Memory paging takes care of the details, and makes sure the parts you are accessing is swapped into RAM.
|
|
|
|
|
thanx for the reply.but i didnt get ur answer completely.....
'and makes sure the parts you are accessing is swapped into RAM'
i didnt follow this line!does this above statement mean that if i were to change the contents of the file ,then these changes are updated in the RAM and finally written on the file?but then if i am making a change in the file directly,then what is the need for this MapViewOfFile ,coz either way i get the change!
can u please elaborate?
"faith, hope, love remain, these three.....; but the greatest of these is love" -1 Corinthians 13:13
|
|
|
|
|
You mean, why use memory mapped files when the ordinary I/O functions also work? The answer is performance. Memory mapped files are extremly efficient if you want to handle large files.
Did that answer your question?
|
|
|
|
|
Niklas Lindquist wrote:
The answer is performance. Memory mapped files are extremly efficient if you want to handle large files.
I dont know about performance, but they definitely provide you simplicity in terms reading and writing files yourselves using the file management APIs.
I have a couple of questions on CreateFileMapping:
- CreateFileMapping returns success if both the handle parameter is NULL, and the Name parameter is NULL. What purpose does this mapping serve?
- How can we change the read-write access of a file mapping object after it has been created? ie., I want to map a file into memory, change it and without closing the mapping object want to view it (only) read-only. Is that possible?
Thanks.
|
|
|
|
|
Thank u for the answer!
cheerz!
"faith, hope, love remain, these three.....; but the greatest of these is love" -1 Corinthians 13:13
|
|
|
|
|
Hi,
I need to start a program with errormode=SEM_NOALIGNMENTFAULTEXCEPT. I have made a small launcher program for this purpose. See here the code:
<br />
SetErrorMode(SEM_NOALIGNMENTFAULTEXCEPT);<br />
PROCESS_INFORMATION pi;<br />
CreateProcess(TEXT("C:/app.exe"), NULL, NULL, NULL, false, 0, NULL, NULL, NULL, &pi);<br />
With this code starts the application app.exe, but i am not sure that it is runnend as child. It need to be a child, otherwise it doesn't use the right errorcode.
Do you know if i have done it right? If not, how can i start a application as child process? Or is there another way todo it?
Thank you.
Best regards,
Willem
|
|
|
|
|
What do you mean by "child process?" Once a process has been started, it is a process in its own right. Both processes are free to do what they will without affecting/impacting the other.
"One must learn from the bite of the fire to leave it alone." - Native American Proverb
|
|
|
|
|
I am developing an MFC application on the .NET compiler.
When I use atof function to convert a string to double value the compiler changes the decimal part of the result.
When I try to add two doubles the compiler does the same.
Below there is a simple example that I tried with the results I got:
example 1:
char str[11];
memset(str,0x00,11);
memcpy(str,"1234567.89",10);
double a = atof(str);
Output : a = 1234567.8899999999
--------------------------------
example 2
double a=1234567;
double b=0.89;
double d = a + b;
Output : a = 1234567.8899999999
Does anyone know why does this happen and what can I do to make it work properly?
Thanks in Advance
sirtimid
|
|
|
|
|
this is not a double problem ; it is a rounding one, known by the iso definition.
TOXCCT >>> GEII power [toxcct][VisualCalc]
|
|
|
|
|
|
|
I don't display it. I see it by the debugger.
|
|
|
|
|
there's not much you can do about it.
there's a book/paper about that :
"What Every Computer Scientist Should Know About Floating Point Arithmetic" by David Goldberg
http://docs.sun.com/source/806-3568/ncg_goldberg.html
Maximilien Lincourt
Your Head A Splode - Strong Bad
|
|
|
|
|
Hi,
I need to write an app, which could trap as soon as some Windows app of the
same computer sends a print job to the network printer server, add some data
to JOB_NOTIFY_FIELD_USER_NAME field of the print job, and then have continue
sending.
I only concern about the new print jobs sent from the computer which run
this app, other than all print queue of the network printer. It seems that
spooler APIs such as FindFirstPrinterChangeNotification, monitor all print
job in the queue.
Is there a simple way for me?
Thank you.
|
|
|
|
|
hello, in a singleton design, i trying to make my parent class having protected constructor and destructor. however, the compiler give me error when my child, trying to delete itself through parent pointer.
isn't child should have access to parent protected destructor?
thank you.
class CMachineFactory
{
protected:
CMachineFactory() {}
virtual ~CMachineFactory() {}
__________________________________________________________
CMachineFactory* CSemiAutoMachineFactory::m_pInstance = NULL;
class CSemiAutoMachineFactory : public CMachineFactory
{
protected:
CSemiAutoMachineFactory();
virtual ~CSemiAutoMachineFactory();
void CSemiAutoMachineFactory::DeleteInstance()
{
if(m_pInstance != NULL) {
delete m_pInstance; // Why not alowed. Isn't child is allowed
// to access parent's memeber?
m_pInstance = NULL;
}
}
E:\xxx\semiautomachinefactory.cpp(214) : error C2248: 'CMachineFactory::~CMachineFactory' : cannot access protected member declared in class 'CMachineFactory'
E:\Work\rd00072\ScanProxyEx\machinefactory.h(43) : see declaration of 'CMachineFactory::~CMachineFactory'
|
|
|
|
|
This is a non-compliant issue with VC++ compiler. The code should
compile, but it will not in VC++ 6.0 or 7.1.
As a workaround, you can create a friend class and let it delete your
singleton.
Here's a workaround example:
template<typename t="">
class Singleton
{
protected:
Singleton(){}
~Singleton(){}
Singleton(const Singleton&);
Singleton& operator=(const Singleton&);
public:
class FriendClass
{
public:
FriendClass():m_MyClass(new T()){}
~FriendClass(){delete m_MyClass;}
T* m_MyClass;
};
static T& Instance() {
static FriendClass Instance;
return *Instance.m_MyClass;
//Will not compile with the following code in VC++ 6.0 or 7.1
// static T Instance;
// return Instance;
}
};
//The above Singleton template can be used two ways.
//Either by deriving from it, or by declaring the friend class with
fully
//qualified template, and calling the instance member function with
fully
//qualified template function.
class Widget {
private:
Widget(){}
~Widget(){}
Widget& operator=(const Widget&);
Widget(const Widget&);
public:
friend class Singleton<widget>::FriendClass;
int m_i;
};
class foo : public Singleton<foo>{
private:
foo(){}
~foo(){}
foo& operator=(const foo&);
foo(const foo&);
public:
friend class FriendClass;
int m_i;
};
int main(int, char*)
{
Widget& MyWidget = Singleton<widget>::Instance();
foo& Myfoo = foo::Instance();
Top ten member of C++ Expert Exchange.
http://www.experts-exchange.com/Cplusplus
|
|
|
|