Jump to content

sc_module/sc_object dynamic creating and delete


Jiang

Recommended Posts

Hello,

I have a access violation issue in my systemc program, the root cause is stated in below link;

Since when the parent's delete called, the kernel seems will colllect its chidren, but its children has already been "delete"'d in parent's DTOR;

But I do want to use dynamic creating of sub components becasue for example, I have a array of same class and want to initiate them in a for loop;

Anyone have idea on this?

 

Link to comment
Share on other sites

Without showing code to illustrate the issue it's hard to gitve suggestions. Therefore some general remarks:

  • there is basically no difference in using a if you create submodules as members (aka on the stack) or as pointer to members (aka on the heap). In the first case the compiler takes a lot of burden from you managing the lifetime
  • You should delete objects in the opposite order than they are created. This is what the compiler does when automatically creating the destructor
  • Avoid raw pointer, use unique_ptr instead. They implicitly manage the lifetime of your objects

The best way to ask for help on such problems if you could provide an example using https://www.edaplayground.com/home

Link to comment
Share on other sites

2 hours ago, Eyck said:

Without showing code to illustrate the issue it's hard to gitve suggestions. Therefore some general remarks:

  • there is basically no difference in using a if you create submodules as members (aka on the stack) or as pointer to members (aka on the heap). In the first case the compiler takes a lot of burden from you managing the lifetime
  • You should delete objects in the opposite order than they are created. This is what the compiler does when automatically creating the destructor
  • Avoid raw pointer, use unique_ptr instead. They implicitly manage the lifetime of your objects

The best way to ask for help on such problems if you could provide an example using https://www.edaplayground.com/home

Hi Eyck and All

Thanks for the advice, and I understand using unique ptr will solve this issue and it did, but stil want to dig this a bit further;

Just look at the stack trace at 2nd image, it's accessing orphan_child_objects() on 3rd image which triggers access on recycled memory area;

By looking at 4th image, this '*it' object 0x17108e935a0 is actually fifo[9], which has already been destroyed in 1st image line 962;

In this case, some of the elements in fifo[] are still there (marked as deleted on heap but still accessible), and some others are not because I think the system recycled them after the delete op;

Why not systemc kernel just wipe out all sub components before it's parent, but instead still need to access them such as in orphan_child_objects()?

In this way I can only use smart pointers, though it's a good idea. If it's true, but seems I can't find this guildline in any systemc documents;

2024-09-12_14-31.png.4f577bc1a2b2220ec9a43cebeecfc809.png

2024-09-12_14-32.png.4dec8d19ea2fdd10a32cb25f86457e4a.png

2024-09-12_14-32_1.thumb.png.3a662a1acb19a32f889e62e2ea912931.png

 

2024-09-12_14-33.thumb.png.5651bd6dd6e618b68520efc6aaead6de.png

2024-09-12_14-36.png.89c91869dd0320fa0606af50c57face3.png

 

 

Edited by Jiang
more explain
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...