Login Page - Create Account

Support Board


Date/Time: Wed, 27 Nov 2024 18:33:16 +0000



[Programming Help] - Dynamic Memory

View Count: 607

[2023-07-23 22:47:07]
WarriorTrader - Posts: 245
Hello,

I have been getting intermittent CPU exception errors on a few studies after upgrading and suscept it is the dynamic arrays I am using. Should I be using sc.AllocateMemory() function?





std::map<int, int> * LabelMemory = (std::map<int, int> *)sc.GetPersistentInt(1);


  if (LabelMemory == NULL) {

    LabelMemory = (std::map<int, int> *) new std::map<int, int>;

  if (LabelMemory != NULL)

    sc.SetPersistentInt(1,(int)LabelMemory);
  else
    return;
  }


//-----------------------------------------------------------------

  struct p_VolumeSlice {
    
    int SliceAsk; //PriorLowAsk
    int SliceBid;
    int SlicePrice;
    
    int LimitOrderBid; //PriorPullBid
    int LimitOrderAsk;
    
    int MarketOrderBid; //PriorInitBid
    int MarketOrderAsk;
    
    int ResponsiveAll; //ResponsiveAll
    
      
  };


p_VolumeSlice * VolumeSlice = (p_VolumeSlice *)sc.GetPersistentInt(62);



  if ((VolumeSlice == NULL) && (ChartReversalNum !=0)) {

    VolumeSlice = (p_VolumeSlice *) new p_VolumeSlice[ChartReversalNum];

    if (VolumeSlice != NULL)

      sc.SetPersistentInt(62,(int)VolumeSlice);
    else
      return;
  }  


I also replaced delete [] LabelMemory; with sc.FreeMemory(LabelMemory);

Using SC Version 2525 Revision 26745.

I initially upgraded to 2512 and got the problem so I upgraded again to 2525.

--WT
Date Time Of Last Edit: 2023-07-23 23:24:07
[2023-07-24 09:54:11]
User431178 - Posts: 544
Useful info here: ACSIL Programming Concepts: Dynamic Memory Allocations Within Study Instance

Suggest replacing sc.GetPersistentInt with sc.GetPersistentPointer, int and pointer not the same size.
[2023-07-24 14:07:05]
WarriorTrader - Posts: 245
Thanks,

sc.GetPersistentPointer was there staring me in the face but I would of never saw that. I was looking at the SC sample code but only saw sc.AllocateMemory() and sc.FreeMemory.

--WT
[2023-07-24 20:38:32]
WarriorTrader - Posts: 245
Had to revert everything back to how it was: sc.AllocateMemory() does not work with a map type. The study compiles but caused a CPU exception at runtime. I have to assume sc.FreeMemory does not work with map either.


The errors went away when I removed 22 stock charts I recently loaded. Will have to get more memory. My computer memory usage is close to 90% with these extra charts and SC uses over 1Gig of memory. I will assume there are plenty of people using over 1 Gig of memory with SC.


There is no documentation on sc.AllocateMemory() but there is some source code in the header file:

  void* AllocateMemory(int Size)
  {
    char* Pointer = (char*)HeapAlloc(GetProcessHeap(), 0, (SIZE_T)Size);

    if (Pointer != NULL)
      memset(Pointer, 0, Size);

    return Pointer;
  }

My guess is the function HeapAlloc or memset do not work with map, but SC Support will have to verify that.

--WT
[2023-07-24 23:35:57]
Sierra_Chart Engineering - Posts: 17198
The Allocate Memory function will allocate memory. But if you are allocating memory for a class object and not using new, you have to ensure the constructor is going to get called.
Sierra Chart Support - Engineering Level

Your definitive source for support. Other responses are from users. Try to keep your questions brief and to the point. Be aware of support policy:
https://www.sierrachart.com/index.php?l=PostingInformation.php#GeneralInformation

For the most reliable, advanced, and zero cost futures order routing, use the Teton service:
Sierra Chart Teton Futures Order Routing
[2023-07-25 00:54:20]
WarriorTrader - Posts: 245
The Allocate Memory function will allocate memory. But if you are allocating memory for a class object and not using new, you have to ensure the constructor is going to get called.




I do not use classes in any of my studies. Like I said it compiles but creates a CPU exception at runtime. The line that I tried is commented out below:

  if (PriceLabelMemory == NULL) {

    PriceLabelMemory = (std::map<int, int> *) new std::map<int, int>;
    //PriceLabelMemory = (std::map<int, int> *) sc.AllocateMemory(sizeof(std::map<int, int>));

  if (PriceLabelMemory != NULL)

    sc.SetPersistentInt(2,(int)PriceLabelMemory); //sc.PersistVars->Integers[2] = (int)PriceLabelMemory;
  else
    return;
  }  

[2023-07-25 01:41:19]
WarriorTrader - Posts: 245
  std::map<int, int> * LabelMemory = (std::map<int, int> *)sc.GetPersistentInt(1); //sc.PersistVars->Integers[1];
  std::map<int, int> * PriceLabelMemory = (std::map<int, int> *)sc.GetPersistentInt(2); //sc.PersistVars->Integers[2];
  //std::map<int, int> * LabelMemory = (std::map<int, int> *)sc.GetPersistentPointer(1);
  //std::map<int, int> * PriceLabelMemory = (std::map<int, int> *)sc.GetPersistentPointer(2);

To post a message in this thread, you need to log in with your Sierra Chart account:

Login

Login Page - Create Account