Re-use AllocFileRequest()

5 posts / 0 new
Last post
mritter0
mritter0's picture
Offline
Last seen: 1 year 10 months ago
Joined: 2014-04-21 21:15
Re-use AllocFileRequest()

I have created an Asl requester, FileReq=AllocFileRequest(), and use it with AslRequestTags().

All is working just fine. I initialize it once, use it several times, then FreeFileRequest(FileReq); at program quit.

It is working fine, but is this a good idea? Should I allocate it every time before opening the requester and free it right after?

jabirulo
jabirulo's picture
Offline
Last seen: 1 day 1 hour ago
Joined: 2013-05-30 00:53
What I do on MIXER (when user

What I do on MIXER (when user chooses 'Change skin...' menu option) is to call this function (and yes, it opens/allocates and frees ASL every time):

  1. BOOL ChangeSkin(void)
  2. {
  3. STRPTR initdrw = IExec->AllocVecTags(1024, TAG_DONE);
  4. BOOL resul;
  5. struct FileRequester *myfilreq = NULL;
  6.  
  7. IUtility->Strlcpy(initdrw, (STRPTR)ARG("IMAGEPATH"), 1024);
  8. //STRPTR fileptr = IDOS->PathPart(initdrw);
  9. //*(fileptr) = '\0';
  10.  
  11. myfilreq = IAsl->AllocAslRequest(ASL_FileRequest, NULL);
  12.  
  13. resul = IAsl->AslRequestTags(myfilreq,
  14. ASLFR_TitleText, GetString(&li, MSG_LOAD_TITLE),
  15. ASLFR_DrawersOnly, TRUE,
  16. ASLFR_InitialDrawer, initdrw,
  17. ASLFR_Screen, MixWindow->WScreen,
  18. TAG_END);
  19. if(resul)
  20. {
  21. if(IUtility->Stricmp((STRPTR)ARG("IMAGEPATH"),myfilreq->fr_Drawer) != 0)
  22. IUtility->Strlcpy( (STRPTR)ARG("IMAGEPATH"), myfilreq->fr_Drawer, 1024 );
  23. else
  24. resul = FALSE;
  25. }
  26.  
  27. IAsl->FreeAslRequest(myfilreq);
  28. IExec->FreeVec(initdrw);
  29.  
  30. return resul;
  31. }

AOS4.1/SAM460ex/PPC460EX-1155MHZ/2048MB/RadeonHD6570/SSD120GB/DVDRW :-P

salass00
salass00's picture
Offline
Last seen: 1 year 1 month ago
Joined: 2011-02-03 11:27
Yes, it is correct and in

Yes, it is correct and in fact recommended to reuse a filereq because that way it will automatically remember what file/directory was selected last.

If you're program supports loading and saving I suggest you use a different one with ASLFR_DoSaveMode set to TRUE for saving. You could reuse the same one setting ASLFR_DoSaveMode in AslRequestTags() to TRUE or FALSE depending on what's needed but it can sometimes be useful to be able to load files from one directory, edit them and then save them to another one without having to change the current directory for the requester every time.

OldFart
OldFart's picture
Offline
Last seen: 6 hours 1 min ago
Joined: 2010-11-30 14:09
@Jabirulo In your code you

@Jabirulo

In your code you make use of functions which return a result, like IExec->AllocVecTags() and IAsl->AllocAslRequest(). You would do well in verifying the result of those functions and only take further actions when the results are 'to your liking' i.e. being non-NULL.

The code as supplied "may" crash, due to f.i. IExec->AllocVecTags() not being able to allocate the required amount of memory, yet you proceed as if it had done so, thereby possibly referencing a NULL-pointer, which will lead to a crash.

OldFart

jabirulo
jabirulo's picture
Offline
Last seen: 1 day 1 hour ago
Joined: 2013-05-30 00:53
I'm a but lazy/newbie/bad

I'm a but lazy/newbie/bad coder and just thought 1KB to alloc won't fail on today AOS4.1 systems ;-) it's a rodiculous amount of memory (I could make it STRPTR initdrw[1024]; and avoid all those mini allocations).

But anyway THX for suggestions.

AOS4.1/SAM460ex/PPC460EX-1155MHZ/2048MB/RadeonHD6570/SSD120GB/DVDRW :-P

Log in or register to post comments