I am a little confused over how to handle memory allocation when
writing new records to a database. I understand that the basic flow
1. open a database handle using DmOpenDatabaseByTypeCreator()
2. create a new record and get a handle to it using DmNewRecord()
3. lock the record using MemHandleLock()
4. fill in the new record with my new data using DmWrite()
5. unlock the record using MemHandleUnlock()
6. release the record handle using DmReleaseRecord()
7. close the database handle using DmCloseDatabase()
First, please correct me if the above is wrong. But assuming it's not
- my problem is with step #4.
Let's say that for example my database records consist of structures
typedef struct MyDatabaseEntryType
Before I can call DmWrite(), I need to create a new instance ofQuote:} MyDatabaseEntryType;
MyDatabaseEntryType and fill in the two string values. Here's the
1. How should I allocate memory for pszValOne and pszValTwo, knowing
I'm going to be putting the overall structure in a database?
2. Should/can I deallocate the memory allocated to pszValOne and
pszValTwo immediately after I call DmWrite()? And if not, when is it
safe to do so? In other words, is DmWrite() making a copy of the data,
or is it simply storing a pointer to it?
To make this more complicated/concrete, what if I am specifically
getting pszValOne and pszValTwo from a form? In other words, I have a
form with two string fields on it and an "ok" button. In the form's
event handler, if the user clicked the "ok" button, I want to write
code to generate a new record in my database to store these two string
values, using the MyDatabaseEntryType struct described above. Can
someone give me a simple code snippet example of how I would do just
this part without causing memory issues?